#javascript #vanilla
// Bookmarks
https://plainjs.com/
// Run code after html loads
function ready () {
// do something...
}
document.addEventListener('DOMContentLoaded', ready)
// Get element by ID
var container = document.getElementById('header');
// Get element by attribute
document.querySelectorAll('[data-foo="value"]')
document.querySelectorAll('#element img')
element.setAttribute('style', 'color: red; border: 1px solid blue;');
// Get elements attribute
document.getAttribute('id')
// Get element by classname
document.getElementsByClassName('test')
// Get elements child
var polys = document.querySelectorAll('#divId1 polygon, #divId1 polyline');
// Get element by tag
element.getElementsByTagName('td');
// Disgard undefined
if (typeof span === 'undefined') {
return
}
// Removing first 3 characters from a string
element.substring(3)
// Get the first 3 characters from a string
element.slice(0, 3)
// Get the last 3 characters from a string
element.slice(-3)
// prevent children elements from getting in the way
// of click events using CSS.
a > * {
pointer-events: none;
}