Skip to content

Instantly share code, notes, and snippets.

@CodeZeno
Created Jun 29, 2020
Embed
What would you like to do?
Javascript - Attach functions to array of elements returned by a selector
const $ = function(selector) {
return new $.elements(Array.from(document.querySelectorAll(selector)));
}
$.elements = function(elements) {
this.html = function(text) {
elements.map((element) => element.innerHTML = text);
};
this.load = async function(filePath) {
this.html(await (await fetch(filePath)).text());
};
return this;
};
// $('title').html("Title changed"); //Sets the innerHTML of the html title element using the tag name
// $('p').html("This is a paragraph"); //Sets all innerHTML of all html p elements using the tag name
// $('#app').load("path/to/file.html"); //Sets the innerHTML of an element with id of 'app' to the contents of a file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment