Created
June 29, 2020 05:29
-
-
Save CodeZeno/929ae5c4cf9e1c280961db1b51e13b0e to your computer and use it in GitHub Desktop.
Javascript - Attach functions to array of elements returned by a selector
This file contains hidden or 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
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