- Interactive effects
- Dynamically modifying web pages
- Changing the HTML the browser originally downloaded
What we're going to do:
- Identify an element on the page
- Do something to the element
Press → key to advance. Zoom in/out: Ctrl or Command + +/-.
What we're going to do:
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>A Basic Web Page</title> </head> <body> <p> <strong>important text</strong> and <a href="#">a link</a>. </p> </body> </html>ancestor | parent | node | textnode | sibling | child | descendent
html ______|____________ | | head body ______|____ | | | | title meta p | ______|_________ | | | | | A basic web page strong and a . | | | | important text a link
document.getElementById(); document.getElementsByTagName(); document.getElementsByClassName(); //IE9 document.querySelector(); //IE8 document.querySelectorAll(); //IE8
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>A Basic Web Page</title> </head> <body <div id="box"> <ul> <li>Some <strong>imp text</strong></li> <li>And <a href="http://x.com">a link</a>.</li> <li class="cl">With a class</li> <li class="cl">another classed</li> <li class="di">Has an ID</li> </ul> </div> </body> </html>
var box = document.getElementById('box'); var box = document.querySelector('#box'); var lis = box.getElementsByTagName('li'); var lis = box.querySelectorAll('li'); var classed = lis.getElementsClassName('cl'); var classed = box.querySelectorAll('.cl'); lis.length? box.length? classed.length?
html (parent) ______|____________ | | head siblings body (children)
p (parent) ______|_________ | | | | strong and a . (children and siblings)
var box = document.querySelector("#box"); var ul = box.getElementsByTagName("ul")[0]; // includes empty text nodes too var kids = ul.childNodes; // text if textnode, value if attribute node, null if element var first = kids[0].nodeValue;
Parents
var lis = box.getElementsByTagName('li'); var ul = lis[1].parentNode;
Siblings
var firstli = lis[1].prevSibling; var thirdli = lis[1].nextSibling;
We need to find a DOM node so that we can add content to it, before it, after it, etc.
<div id="changeme"> <ul> <li>Some <strong>imp text</strong></li> <li>And <a href="http://x.com">a link</a>.</li> <li class="cl">With a class</li> <li class="cl">another classed</li> <li class="di">Has an ID</li> </ul> </div>
//find elements var lis = document.querySelectorAll('#changeme li'); //change the content of one lis[2].innerHTML = 'JavaScript Rocks';
//find elements var lis = document.querySelectorAll('#changeme li'); //change the content of one lis[2].innerHTML = 'JavaScript Rocks'; //find another elements var strong = lis[0].getElementsByTagName('strong'); //change that content too strong[0].innerHTML = 'emboldened content';
var newLI = document.createElement('li'); newLI.setAttribute('class', 'myClass'); var textNode = document.createTextNode("some text"); newLI.appendChild(textNode); var myUL = document.querySelector("#changeme ul"); myUL.appendChild(newLI);
var lis = document.querySelectorAll('#changeme li'); lis[4].parentNode.removeChild(lis[4]);
var box = document.getElementById('changeme'); var list = box.getElementsByTagName('ul')[0]; var fifthitem = box.getElementsByTagName('li')[4]; var item = list.removeChild(fifthitem);
Slide 11 displays the text created by the markup on slide 10. Let's play with it! In the console on slide 11 do the following: