Skip to content

Instantly share code, notes, and snippets.

@chapuzzo
Forked from paulirish/bling.js
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save chapuzzo/7b25e8f0d4fa0a0ac0b4 to your computer and use it in GitHub Desktop.

Select an option

Save chapuzzo/7b25e8f0d4fa0a0ac0b4 to your computer and use it in GitHub Desktop.
// bling
// the kind of thing you toss into the page as you start prototyping
Window.prototype.$ = document.querySelectorAll.bind(document)
Node.prototype.on = Window.prototype.on = function (name, fn) {
this.addEventListener(name, fn)
}
// NodeList needs all Array goodies
NodeList.prototype.__proto__ = Array.prototype
// NodeList needs EventTarget goodies but iterated
NodeList.prototype.on = NodeList.prototype.addEventListener = function (name, fn) {
this.forEach(function (elem, i) {
elem.on(name, fn)
})
} //
// Notes:
// it's all resig's fault. github.com/jeresig/nodelist
// setting `Node.prototype.on = EventTarget.prototype.addEventListener` would be sweet but, no funciona in IE/Saf
// not done: dispatchEvent & removeEventListener shortcuts, but who caaares
// We should be able to forEach on the qSA result.
document.querySelectorAll('input').forEach(function (el) {
el.readOnly = true
})
// addEventListhistakestoolongtotype. on() method
document.body.on('dblclick', function (e) {
console.log('double clicked the body')
})
// classic Bling + on()
$('p').on('click', function (e) {
e.currentTarget.hidden = true
})
function simulateClick(elem) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
var canceled = !elem.dispatchEvent(evt);
}
var input1 = document.createElement('input');
input1.id = 'one';
document.body.appendChild(input1);
var input2 = document.createElement('input');
input2.id = 'two';
document.body.appendChild(input2);
$('input').on('click', function(e) {
console.log('PASS!', e.target.id, ' clicked! ');
})
// either click 'em or this is for testing…
function simulateClick(elem) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
var canceled = !elem.dispatchEvent(evt);
}
simulateClick(input1)
setTimeout(function(){ simulateClick(input2) }, 500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment