Skip to content

Instantly share code, notes, and snippets.

@faaezahmd
Created December 5, 2018 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faaezahmd/daa5ddc4b2be315dd6f03479a35342ea to your computer and use it in GitHub Desktop.
Save faaezahmd/daa5ddc4b2be315dd6f03479a35342ea to your computer and use it in GitHub Desktop.
function select(selector) {
// Checks select(01) and select('')
if(typeof selector !== 'string' || selector.length < 2) {
return false;
}
var el = document.querySelector(selector) || null;
var allEl = document.querySelectorAll(selector) || [];
if (el === null && allEl.length === 0) {
return false;
}
if(allEl.length === 1) {
return el;
} else {
return allEl;
}
}
const btn = select('.ad');
if (btn) {
btn.addEventListener('click', (e) => {
console.log(e);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment