Last active
May 21, 2024 10:20
-
-
Save artemsites/2c273b4f894aacfb59df221cc63aa683 to your computer and use it in GitHub Desktop.
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
/** | |
* @title Функции помощники для быстрого кодинга, лучшей читаемости и уменьшения итоговой сборки | |
* | |
* @version 1.3 | 20240521 | |
* @author https://t.me/artemsites | |
* | |
* @param {string} selector - CSS selector | |
* @param {Element} wrapper - Element wrapper, default document.documentElement | |
* @returns {Element|NodeList} | |
* | |
* @example getEl('.c') ->(after bunling)-> document.querySelector('.c') | |
* @example getEl('.c', document.body) ->(after bunling)-> document.querySelector('.c') | |
* | |
* document.querySelector('.c') ->(after bunling)-> document.querySelector('.c') | |
* getEl('.c') ->(after bunling)-> g('.c') | |
* | |
* import { getElById } from "/src/utils/dom.js" | |
*/ | |
export function getEl(selector, wrapper = document) { | |
let res = wrapper.querySelectorAll(selector) | |
if (res.length===1) return res[0] | |
else if (res.length===0) return false | |
return res | |
} | |
export function getElById(id) { | |
return document.getElementById(id) || null | |
} | |
export function addClassOn(className, el) { | |
el.classList.add(className) | |
} | |
export let addClassTo = addClassOn | |
export function addClassesOn(classesNames, el) { | |
classesNames.split(' ').forEach((className)=>{ | |
addClassOn(className, el) | |
}) | |
} | |
export let addClassesTo = addClassesOn | |
export function removeClassOn(className, el) { | |
el.classList.remove(className) | |
} | |
export let removeClassFrom = removeClassOn | |
export function toggleClassOn(className, el) { | |
el.classList.toggle(className) | |
} | |
export function containsClassOn(className, el) { | |
return el.classList.contains(className) | |
} | |
export function doByOn(callback, event, el) { | |
el.addEventListener(event, callback) | |
} | |
export function textTo(text, el) { | |
el.textContent = text | |
} | |
export let setTextTo = textTo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment