File tree Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Class for managing HTML elements with specific attributes.
3+ */
4+ class HtmlAttributeManager {
5+ /**
6+ * Creates an instance of HtmlAttributeManager.
7+ * @param {string } attributeName - The name of the HTML attribute to search for.
8+ * @param {string } functionName - The name of the function to call for each matching element.
9+ */
10+ constructor ( attributeName , functionName ) {
11+ this . attributeName = attributeName ;
12+ this . functionName = functionName ;
13+ this . handleEvent = this . handleEvent . bind ( this ) ;
14+ document . addEventListener ( 'DOMContentLoaded' , this . handleEvent ) ;
15+ }
16+
17+ /**
18+ * Finds HTML elements with the specified attribute and calls the corresponding function.
19+ */
20+ handleEvent ( ) {
21+ try {
22+ const elements = document . querySelectorAll ( `[${ this . attributeName } ]` ) ;
23+ elements . forEach ( element => {
24+ const attributeValue = element . getAttribute ( this . attributeName ) ;
25+ if ( typeof window [ this . functionName ] === 'function' ) {
26+ window [ this . functionName ] ( element , attributeValue ) ;
27+ } else {
28+ console . error ( `Function '${ this . functionName } ' not found or not a function.` ) ;
29+ }
30+ } ) ;
31+ } catch ( error ) {
32+ console . error ( 'An error occurred while processing HTML attributes:' , error ) ;
33+ }
34+ }
35+
36+ /**
37+ * Removes the event listener when the instance is no longer needed.
38+ */
39+ destroy ( ) {
40+ document . removeEventListener ( 'DOMContentLoaded' , this . handleEvent ) ;
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " html-attribute-manager" ,
3+ "version" : " 1.0.0" ,
4+ "description" : " A JavaScript library for managing HTML elements with specific attributes." ,
5+ "main" : " index.js" ,
6+ "scripts" : {
7+ "test" : " echo \" Error: no test specified\" && exit 1"
8+ },
9+ "repository" : {
10+ "type" : " git" ,
11+ "url" : " git+https://github.com/oguzhan18/html-attribute-manager.git"
12+ },
13+ "keywords" : [
14+ " html" ,
15+ " attribute" ,
16+ " manager" ,
17+ " javascript"
18+ ],
19+ "author" : " Your Name" ,
20+ "license" : " ISC" ,
21+ "bugs" : {
22+ "url" : " https://github.com/oguzhan18/html-attribute-manager/issues"
23+ },
24+ "homepage" : " https://github.com/oguzhan18/html-attribute-manager#readme"
25+ }
You can’t perform that action at this time.
0 commit comments