Skip to content

Instantly share code, notes, and snippets.

@stayradiated
Last active June 27, 2022 10:00
Show Gist options
  • Save stayradiated/5617498 to your computer and use it in GitHub Desktop.
Save stayradiated/5617498 to your computer and use it in GitHub Desktop.
Using native methods instead of jQuery
class $
constructor: (query) ->
return document.querySelector(query)
@find: (parent, query) ->
parent.querySelector(query)
@findAll: (parent, query) ->
parent.querySelectorAll(query)
@create: (tag) ->
document.createElement(tag)
@append: (el) ->
document.body.appendChild(el)
@prepend: (el) ->
document.body.insertBefore(el, document.body.firstChild)
@class: (className) ->
document.getElementsByClassName(className)
@all: (query) ->
document.querySelectorAll(query)
@addClass: (el, className) ->
el.classList.add(className)
@removeClass: (el, className) ->
el.classList.remove(className)
@toggleClass: (el, className, value) ->
el.classList.toggle(className, value)
@hasClass: (el, className) ->
el.classList.contains(className)
@css: (el, prop, val) ->
el.style[prop] = val
module.exports = $
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment