Last active
March 1, 2017 17:21
-
-
Save bradoyler/0330222840b549ae88eb86b8ba6c9d4c to your computer and use it in GitHub Desktop.
DomUtils library. Selection (.selectAll), Iterator (.each), Event Delegation (.on)
This file contains 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
// Work in progres.... | |
// Intended for Browsers IE11+ | |
// demo: http://jsbin.com/foquned/edit?js,console,output | |
var DomUtils = { | |
selectAll: function (selector) { | |
var list = []; | |
var myNodeList = document.querySelectorAll(selector); | |
this.each(myNodeList, function (index, value) { | |
list.push(value); | |
}); | |
return list; | |
}, | |
each: function (array, callback, scope) { | |
for (var i = 0; i < array.length; i++) { | |
callback.call(scope, i, array[i]); // passes back stuff we need | |
} | |
}, | |
// not fully working | |
on: function(type, selector, handler) { | |
document.querySelector(selector).addEventListener(type, handler); | |
} | |
} | |
// testcase 1: select html from 1st element | |
var test = DomUtils.selectAll('.test'); | |
console.log('>> '+ test[0].innerHTML); | |
// testcase 2: add click event to selected element | |
DomUtils | |
.on('click', '.test', function() { | |
console.log('click'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
demo: http://jsbin.com/foquned/edit?js,console,output
using http://zeptojs.com/ for inspiration.