DOM Enlightenment Book
HTML
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
<ul>
<li>2</li>
<li>3</li>
</ul>
<h4 style="border-top:1px solid #ADADAD;padding-top:20px;margin-top:20px;"><strong>← Code Example from <a href="http://www.domenlightenment.com/" target="_blank">DOM Enlightenment Book</a> by <a href="http://www.codylindley.com" target="_blank">Cody Lindley</a></strong></h4>
<p>JavaScript code is executed on page load. Click the "Run" button in the top bar to re-run the JavaScript code. All <code>console.log()'s</code> are logged to the <a href="http://getfirebug.com/firebuglite" target="_blank">Firebug Lite</a> console below ↓.</p>
JavaScript
//create a text node and li element node and append the text to the li
var text1 = document.createTextNode('1');
var text2 = document.createTextNode('4');
var li = document.createElement('li');
li.appendChild(text1);
li.appendChild(text2);
//select the ul in the document
var ul = document.querySelector('ul');
/*
add the li element we created above to the DOM, notice I call on <ul> and pass reference to <li>2</li> using ul.firstChild
*/
ul.insertBefore(li,ul.firstChild);
ul.insertAfter(li,ul.firstChild);
console.log(document.body.innerHTML);
/*logs
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
*/