Last active
February 19, 2019 09:54
-
-
Save antonagestam/f8ff54df1bc94839b35bc4d44e92e27e to your computer and use it in GitHub Desktop.
Wrapper around NodeList to easily implement a jQuery-like API in a minimal way
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
<script type="module"> | |
import $ from './dollar.js'; | |
$('a').on('click', (e) => { | |
e.preventDefault(); | |
console.log(e.currentTarget); | |
alert('ello'); | |
}); | |
</script> |
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
export default (function () { | |
class $NodeList { | |
constructor (node_list) { | |
this.node_list = node_list; | |
} | |
on (event_name, event_handler) { | |
// Attach an event listener to each node of the NodeList | |
for (let node of this) | |
node.addEventListener(event_name, event_handler); | |
return this; | |
} | |
[Symbol.iterator] () { | |
return this.node_list[Symbol.iterator](); | |
} | |
first () { | |
return this.get(0); | |
} | |
get (index) { | |
return this.node_list[index]; | |
} | |
} | |
return (selector) => { | |
if (selector === document) | |
return new $NodeList([document]); | |
return new $NodeList(document.querySelectorAll(selector)); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment