Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Last active March 1, 2017 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradoyler/0330222840b549ae88eb86b8ba6c9d4c to your computer and use it in GitHub Desktop.
Save bradoyler/0330222840b549ae88eb86b8ba6c9d4c to your computer and use it in GitHub Desktop.
DomUtils library. Selection (.selectAll), Iterator (.each), Event Delegation (.on)
// 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');
});
@bradoyler
Copy link
Author

bradoyler commented Mar 1, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment