Created
June 21, 2019 09:01
-
-
Save Remeic/10e496b689e031942380bb7a1dc8dfef to your computer and use it in GitHub Desktop.
Simple Dom Operation
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
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