DOM manipulation cheat sheet code example
Example 1: javascript dom cheat sheet
document.getElementById('someid');
document.getElementsByClassName('someclass');
document.getElementsByTagName('LI');
document.querySelector('.someclass');
document.querySelectorAll('div.note, div.alert');
Example 2: dom javascript cheat sheet
var newHeading = document.createElement('h1');
var newParagraph = document.createElement('p');
var h1Text= document.createTextNode('This is a nice header text!');
var pText= document.createTextNode('This is a nice paragraph text!');
newHeading.appendChild(h1Text);
newParagraph.appendChild(pText);
Example 3: dom javascript cheat sheet
var firstHeading = document.getElementById('firstHeading');
firstHeading.appendChild(newHeading);
firstHeading.appendChild(newParagraph);
var parent = firstHeading.parentNode;
parent.insertBefore(newHeading, firstHeading);
Example 4: dom javascript cheat sheet
<div id='box1'>
<p>Some example text</p>
</div>
<div id='box2'>
<p>Some example text</p>
</div>
Example 5: dom javascript cheat sheet
var stored = document.getElementById('someid');
var children = stored.childNodes;
var parental = children.parentNode;