Created
March 12, 2014 05:46
-
-
Save arnabdas/9501538 to your computer and use it in GitHub Desktop.
DOM manipulation in 100 lines (AbsurdJS component)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var dom = function(el, parent) { | |
| var api = { el: null }; | |
| var qs = function(selector, parent) { | |
| parent = parent || document; | |
| return parent.querySelector(selector); | |
| }; | |
| var qsa = function(selector, parent) { | |
| parent = parent || document; | |
| return parent.querySelectorAll(selector); | |
| }; | |
| switch(typeof el) { | |
| case 'string': | |
| parent = parent && typeof parent === 'string' ? qs(parent) : parent; | |
| api.el = qs(el, parent); | |
| break; | |
| case 'object': | |
| if(typeof el.nodeName != 'undefined') { | |
| api.el = el; | |
| } else { | |
| var loop = function(value, obj) { | |
| obj = obj || this; | |
| for(var prop in obj) { | |
| if(typeof obj[prop].el != 'undefined') { | |
| obj[prop] = obj[prop].val(value); | |
| } else if(typeof obj[prop] == 'object') { | |
| obj[prop] = loop(value, obj[prop]); | |
| } | |
| } | |
| delete obj.val; | |
| return obj; | |
| }; | |
| var res = { val: loop }; | |
| for(var key in el) { | |
| res[key] = dom.apply(this, [el[key], parent]); | |
| } | |
| return res; | |
| } | |
| break; | |
| } | |
| api.val = function(value) { | |
| if(!this.el) return null; | |
| var set = !!value; | |
| var useValueProperty = function(value) { | |
| if(set) { this.el.value = value; return api; } | |
| else { return this.el.value; } | |
| }; | |
| switch(this.el.nodeName.toLowerCase()) { | |
| case 'input': | |
| var type = this.el.getAttribute('type'); | |
| if(type == 'radio' || type == 'checkbox') { | |
| var els = qsa('[name="' + this.el.getAttribute('name') + '"]', parent); | |
| var values = []; | |
| for(var i=0; i<els.length; i++) { | |
| if(set && els[i].checked && els[i].value !== value) { | |
| els[i].removeAttribute('checked'); | |
| } else if(set && els[i].value === value) { | |
| els[i].setAttribute('checked', 'checked'); | |
| els[i].checked = 'checked'; | |
| } else if(els[i].checked) { | |
| values.push(els[i].value); | |
| } | |
| } | |
| if(!set) { return type == 'radio' ? values[0] : values; } | |
| } else { | |
| return useValueProperty.apply(this, [value]); | |
| } | |
| break; | |
| case 'textarea': | |
| return useValueProperty.apply(this, [value]); | |
| break; | |
| case 'select': | |
| if(set) { | |
| var options = qsa('option', this.el); | |
| for(var i=0; i<options.length; i++) { | |
| if(options[i].getAttribute('value') === value) { | |
| this.el.selectedIndex = i; | |
| } else { | |
| options[i].removeAttribute('selected'); | |
| } | |
| } | |
| } else { | |
| return this.el.value; | |
| } | |
| break; | |
| default: | |
| if(set) { | |
| this.el.innerHTML = value; | |
| } else { | |
| if(typeof this.el.textContent != 'undefined') { | |
| return this.el.textContent; | |
| } else if(typeof this.el.innerText != 'undefined') { | |
| return typeof this.el.innerText; | |
| } else { | |
| return this.el.innerHTML; | |
| } | |
| } | |
| break; | |
| } | |
| return set ? api : null; | |
| }; | |
| return api; | |
| }; | |
| console.clear(); | |
| console.log(dom('p').el.nodeName); | |
| console.log(dom('p', 'header').el.nodeName); | |
| console.log(dom(document.querySelector('header')).el.nodeName); | |
| console.log(dom({ | |
| footer: 'footer', | |
| paragraphs: { | |
| header: 'header p', | |
| footer: 'footer p' | |
| } | |
| })); | |
| console.log("\n\n"); | |
| console.log(dom('[type="text"]').val()); | |
| console.log(dom('[type="radio"]').val()); | |
| dom('[type="radio"]').val('B'); | |
| // dom('[type="text"]').val('Krasimir Tsonev'); | |
| console.log(dom({ | |
| name: '[type="text"]', | |
| data: { | |
| options: '[type="radio"]', | |
| count: 'select' | |
| }, | |
| version: 'footer' | |
| }, 'form').val()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>JS Bin</title> | |
| </head> | |
| <body> | |
| <p>text</p> | |
| <header> | |
| <p>Big</p> | |
| </header> | |
| <footer> | |
| <p>Small</p> | |
| </footer> | |
| <form action=""> | |
| <input type="text" value="sample text" /> | |
| <input type="radio" name="options" value="A"> | |
| <input type="radio" name="options" checked value="B"> | |
| <select> | |
| <option value="10"></option> | |
| <option value="20"></option> | |
| <option value="30" selected></option> | |
| </select> | |
| <footer>version: 0.3</footer> | |
| </form> | |
| </body> | |
| </html> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Got this at: http://flippinawesome.org/2014/03/10/a-dom-manipulation-class-in-100-lines-of-javascript/