Last active
May 27, 2024 13:43
-
-
Save av01d/424add0af76ecdd49bdf3af337ba466c to your computer and use it in GitHub Desktop.
Javascript: tiny jQuery
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
// Select single element | |
const $ = (e, parent = document) => parent.querySelector(e); | |
// Select multiple elements | |
const $$ = (e, parent = document) => Array.from(parent.querySelectorAll(e)); | |
// Create element | |
const cE = (tagName, props) => { | |
const el = document.createElement(tagName); | |
for (let prop of ['id','name','value','className','type','href','method','action']) { | |
// Direct properties | |
(prop in props) && (el[prop] = props[prop]); | |
} | |
('css' in props) && (el.style.cssText = props.css); | |
('text' in props) && (el.textContent = props.text); | |
('html' in props) && (el.innerHTML = props.html); | |
('onclick' in props) && (el.addEventListener('click', props.onclick)); | |
return el; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: