Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Dom Operation
function elementExists(element) {
return document.body.contains(element);
}
function getElementById(element) {
return document.getElementById(element);
}
function getElementsByClass(element) {
return document.getElementsByClassName(element);
}
function domIsReady(callback) {
// in case the document is already rendered
if (document.readyState != "loading") {
callback();
}
// modern browsers
else if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", callback);
}
}
function removeClass(element, className) {
let result = false;
if (element.classList.contains(className)) {
element.classList.remove(className);
result = true;
}
return result;
}
function addClass(element, className) {
let result = false;
if (!element.classList.contains(className)) {
element.classList.add(className);
result = true;
}
return result;
}
function isMobile() {
if (
/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) &&
screen.width < 768
) {
return true;
} else {
return false;
}
}
function onClick(element, callback) {
element.addEventListener("click", callback(), false);
}
export {
elementExists,
getElementById,
getElementsByClass,
domIsReady,
isMobile,
onClick,
removeClass,
addClass
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.