diff --git a/README.md b/README.md index 36793ab..8f81938 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ new HtmlGenerator ([ tag: "div", val: "Value", att: {class: "class-name"}, + events: { 'click': function(){console.log('clicked')}, 'mouseover': function(){console.log('mouseover')}}, childs: [ { tag: "div", diff --git a/htmlGenerator.js b/htmlGenerator.js index 8c52176..0f13406 100644 --- a/htmlGenerator.js +++ b/htmlGenerator.js @@ -4,14 +4,15 @@ ( global = global || self, global.HtmlGenerator = factory()); }( this, function () { - function _gernerateHtml( listObjectDOM, parentElement ) { - parentElement = parentElement || document.body + function _generateHtml( listObjectDOM, parentElement ) { + parentElement = parentElement || document.body; if (typeof listObjectDOM === 'object' ? Array.isArray(listObjectDOM) : null) { listObjectDOM.forEach(e => { - let tag = document.createElement(e.tag) - _setAtt(tag, e.att) - if (e.val !== undefined) _setValue(tag, e.val) - if (e.childs !== undefined) new HtmlGenerator(e.childs, tag) + let tag = document.createElement(e.tag); + _setAtt(tag, e.att); + if (e.val !== undefined) _setValue(tag, e.val); + if (e.childs !== undefined) new HtmlGenerator(e.childs, tag); + if (e.events !== undefined) _setEvent(tag, e.events); parentElement.appendChild(tag) }); } @@ -20,37 +21,45 @@ function _setAtt( tag, attributes ) { for (const key in attributes) { if (attributes.hasOwnProperty(key)) { - tag.setAttribute(key, attributes[key]) + tag.setAttribute(key, attributes[key]); } } } function _setValue( tag, value ) { - tag.textContent = value + tag.textContent = value; } + function _setEvent(tag, events) { + for (const key in events) { + if (events.hasOwnProperty(key)) { + tag.addEventListener(key, events[key]); + } + } + } - function _initFunctios( hg ) { - hg.setAttributes = function( tag, attributes ) { _setAtt( tag, attributes ) } - hg.setValue = function( tag, value ) { _setValue( tag, value ) } + function _initFunctions( hg ) { + hg.setAttributes = function( tag, attributes ) { _setAtt( tag, attributes ) }; + hg.setValue = function( tag, value ) { _setValue( tag, value ) }; + hg.setEvent = function( tag, events ) { _setEvent( tag, value ) }; } function init( HtmlGenerator ) { HtmlGenerator.prototype.generate = function ( listObjectDOM, parentElement ) { - let hg = this - _gernerateHtml( listObjectDOM, parentElement ) - _initFunctios(hg) - } + let hg = this; + _generateHtml( listObjectDOM, parentElement ); + _initFunctions(hg); + }; } - init(HtmlGenerator) + init(HtmlGenerator); function HtmlGenerator( listObjectDOM, parentElement = null ) { - if (this instanceof HtmlGenerator && listObjectDOM != null){ - this.generate( listObjectDOM, parentElement ) + if (this instanceof HtmlGenerator && listObjectDOM != null) { + this.generate( listObjectDOM, parentElement ); } } - return HtmlGenerator + return HtmlGenerator; -})) +})); diff --git a/test.html b/test.html index 2c3a751..28a2b19 100644 --- a/test.html +++ b/test.html @@ -40,7 +40,8 @@ { tag: "div", att: { class: "class-name-3" }, - val: "Value 3" + val: "Value 3", + events: { 'click': function(){console.log('click')}} } ], document.body)