function grabElement(source, context = document) {
if (typeof source === 'string') {
return context.querySelector(source)
} else {
return source
}
}
A handy set of helpers for use in miscellaneous projects. You can import all of them or just the ones you need.
A shortcut to document.querySelector()
but allows you to
send in an already “grabbed” element (which will then be
returned verbatim).
import { grabElement as $ } from './vendor/helpers'
const logo = $('.logo')
const bigLogo = '.container > .biggie'
showMe(logo)
showMe(bigLogo)
function showMe(element) {
$(element).classList.add('showing')
}
function grabElement(source, context = document) {
if (typeof source === 'string') {
return context.querySelector(source)
} else {
return source
}
}
Same as grabElement()
but using querySelectorAll()
instead.
import { grabAll as $$ } from './vendor/helpers'
const cards = $$('main > .card')
function grabAll(source, context = document) {
if (typeof source === 'string') {
return context.querySelectorAll(source)
} else {
return source
}
}
Export all the functions
export { grabElement, grabAll }