Skip to content

Instantly share code, notes, and snippets.

@bloodyowl
Created May 19, 2015 23:46
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 bloodyowl/05a1b3497dddd32a5adf to your computer and use it in GitHub Desktop.
Save bloodyowl/05a1b3497dddd32a5adf to your computer and use it in GitHub Desktop.
ES7 bindings
function first() {
if(!this.length) {
return this[0]
}
throw new Error("empty list")
}
export default Array.prototype
export function $(selector, root = document) {
return Array.from(root.querySelectorAll(selector))
}
export function getAttribute(attributeName) {
return this::first().getAttribute(attributeName)
}
export function setAttribute(attributeName, value) {
this.forEach((item) => {
item.setAttribute(attributeName, value)
})
return this
}
export function setProperties(properties) {
this.forEach((item) => {
Object.assign(item, properties)
})
return this
}
export function getProperty(property) {
return this::first()[attributeName]
}
export function setInnerHTML(string) {
this.forEach((item) => {
item.innerHTML = string
})
return this
}
export function remove() {
this.forEach((item) => {
const parent = item.parentNode
if(parent) {
parent.removeChild(item)
}
})
return this
}
import {$, setAttribute, setInnerHTML} from "./dom"
$("div")
::setAttribute("data-foo", "bar")
::setInnerHTML("hey")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment