for loop for dom

access all of 1 element in the dom

by Adam Kinnucane

HTML

<h1>
    <a href="http://slipsum.com/">Samuel L Ipsum</a>
</h1>

<p>Wanted to spice up the links around <a href="http://hakim.se">my site</a> and ended up re-creating this over-the-top Flash classic.</p>

<p>Do you see any <a href="http://pbskids.org/teletubbies/">Teletubbies</a> in here? Do you see a slender <a href="http://en.wikipedia.org/wiki/Plastic">plastic</a> tag clipped to my shirt with my name printed on it? Do you see a little Asian child with a blank expression on his face sitting outside on a <a href="http://en.wikipedia.org/wiki/Helicopter">mechanical helicopter</a>that shakes when you put quarters in it? No? Well, that's what you see at a toy store. And you must think you're in a toy store, because you're here shopping for an infant named Jeb.</p>

<p>Well, the way they make shows is, they make one show. That show's called a <a href="http://en.wikipedia.org/wiki/Television_pilot">pilot</a>. Then they show that show to the people who make shows, and on the strength of that one show they decide if they're going to make more shows. Some pilots get picked and become <a href="http://en.wikipedia.org/wiki/Television_program">television programs</a>. Some don't, become nothing. She starred in one of the ones that became nothing.</p>

JavaScript

function getSelector(selector) {
 	//the variable/arr the iterations are going into need to be outside the loop.
   var arr = [];
	//this variable declared in the parameter of the function is used here to select the 			elements you wish to grab.
   var nodes = document.querySelectorAll(selector);
	//var i is iterator
	//var len to hold of all the all of the elements
   for (var i = 0, len = nodes.length; i < len; i++) {
		//iteration of all the nodes
     var node = nodes[i];
	  //nodes is the object of the a tag
	  //therefore . notation to access the href
	  //pushing these into the array.
     arr.push(node.href);
   }
	//console log the array
   console.log(arr)
 }
 
 //calling the function.
 getSelector('a');