Skip to content

Instantly share code, notes, and snippets.

@CodeZeno
Created June 29, 2020 05:29
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 CodeZeno/929ae5c4cf9e1c280961db1b51e13b0e to your computer and use it in GitHub Desktop.
Save CodeZeno/929ae5c4cf9e1c280961db1b51e13b0e to your computer and use it in GitHub Desktop.
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