vanilla JS Flashcards
Select DOM elements by CSS selector
var matches = // elements with a div class="foo" document.querySelectorAll('div.foo');
for (i=0; i
Select elements by class name
var list = document.getElementsByClassName(‘foo’);
// get the number of selected elements console.log(list.length);
// iterate over elements and output their HTML content for (var i=0; i
Select elements by tag name
var list = // METHOD 1 document.getElementsByTagName('a');
// get the number of selected elements console.log(list.length);
// iterate over elements and output href attribute values for (var i=0; i
Select an element by ID
var el = document.getElementById('foo'); el.innerHTML = 'Hello World!';
Get parent element node
var el = document.querySelector('div'); var parent = el.parentNode;
Get siblings of an element
// METHOD 1 function getSiblings(el, filter) { var siblings = []; el = el.parentNode.firstChild; do { if (!filter || filter(el)) siblings.push(el); } while (el = el.nextSibling); return siblings; }
// example filter function function exampleFilter(el) { return elem.nodeName.toLowerCase() == 'a'; }
// usage el = document.querySelector('div'); // get all siblings of el var sibs = getSiblings(el); // get only anchor element siblings of el var sibs_a = getSiblings(el, exampleFilter);
// METHOD 2 var previous = el.previousSibling;
var next = el.nextSibling;
Getting the children of a DOM element.
var el = document.querySelector(‘div’);
var children = el.childNodes, number_of_children = children.length;
for (var i=0; i
Create a DOM element
var el = document.createElement(‘div’);
el.innerHTML = ‘<p>Hello World!</p>’;
// alternatively, var textnode = document.createTextNode('Hello World!'); el.appendChild(textnode);
Replace a DOM element
var el = document.querySelector(‘div’);
// <a href="/javascript/manipulation/creating-a-dom-element-51/">create a new element</a> that will take the place of "el" var newEl = document.createElement('p'); newEl.innerHTML = '<b>Hello World!</b>';
// replace el with newEL el.parentNode.replaceChild(newEl, el);
Unwrap a DOM element
Remove the parents of an element from the DOM, leaving the element’s content in place.
// select element to unwrap var el = document.querySelector('div');
// get the element's parent node var parent = el.parentNode;
// move all children out of the element while (el.firstChild) parent.insertBefore(el.firstChild, el);
// remove the empty element parent.removeChild(el);
Empty an element’s content
Remove all child nodes of an element from the DOM.
var el = document.querySelector('div'); el.innerHTML = '';
Removing an element
var el = document.querySelector('div'); // select the first returned <div> element el.parentNode.removeChild(el);</div>
Insert an element after or before another
function insertAfter(el, referenceNode) { referenceNode.parentNode.insertBefore(el, referenceNode.nextSibling); }
// example var newEl = document.createElement('div'); newEl.innerHTML = '<p>Hello World!</p>';
var ref = document.querySelector('div.before'); insertAfter(newEl, ref);
// insert before function insertBefore(el, referenceNode) { referenceNode.parentNode.insertBefore(el, referenceNode); }
// example var newEl = document.createElement('div'); newEl.innerHTML = '<p>Hello World!</p>';
var ref = document.querySelector('div.before'); insertBefore(newEl, ref);
Get the text content of an element
Get the combined text contents of an element, including its descendants, or set the text content of the element.
var el = document.querySelector('div'); text = el.textContent || el.innerText; console.log(text);
Get and set the HTML content of an element
Read or write the HTML content of an element.
var el = document.querySelector(‘div’);
// get HTML content console.log(el.innerHTML);
// set HTML content el.innerHTML = '<p>Hello World!</p>'