getElementByTagName allows you to search all the elements with a specified tag name on your page. Pictorially it is like an array holding elements.
var myLinkcollection =document.getElementsByTagName(“abc”);
usecase
var myLinkCollection =document.getElementsByTagName(“abc”);for (i =0; i <myLinkCollection.length; i++) {if (myLinkCollection[i].className == “std_class”) { myLinkCollection[i].onclick=function() {this.style.backgroundColor = “#f00”; } }}
querySelector / querySelectorAll
Node
Any element on a page including text & whitespaces of a DOM structure is known as “NODE.” Nodes can be between XHTML Tags.
Nodes available in DOM:
node.childNodes node.firstChild node.lastChild node.parentNode node.nextSibling node.previousSibling Suppose we have following XHTML:
<ulid-“list”> <li><ahref=”prod1.html”class=”list_one”> Product Number One</a></li> <li><ahref=”prod2.html”> Product Number Two</a></li> <li><ahref=”prod3.html”> Product Number Three</a></li> <li><ahref=”prod4.html”> Product Number Four</a></li></ul>
Now if we want to access “Prod1” we can do so using 2 ways:
Using ChildNodes:
var myLinkList =document.getElementsById(“list”)var myFirstProduct =myLinklist.childNodes[0].childNodes[0];alert(myFirstLink.className);
Using firstChild:
var myLinkList =document.getElementsById(“list”)var myFirstProduct =myLinklist.firstChild.firstChild;alert(myFirstLink.className);
createElement
used to create an element & place it anywhere in the DOM structure
var myNewListItem =document.createElement(“li”)var myNewProd =document.createElement(“prod5”);
appendChild
We will add two elements to our list of links using appendChild.
var myNewListItem =document.createElement(“li”)var myNewProd =document.createElement(“prod5”);var myLinkList =document.getElementById(“List”)myLinkList.appendChild(myNewListItem);myLinkList.LastChild.appendChild(myNewProd);
removeChild
var myLinkList =document.getElementById(“list”)var myRemovedLink =myLinkList.lastChild;myLinkList.removeChild(myRemoveLink);
getAttribute
With the getAttribute method, you can access the value of any attribute of an element on a page.
var myLinkFive =document.getElementById(“Prod_5”);var myLinkAttribute =myLinkFive.getAttribute(“abc”);
setAttribute
A useful method to replace values in the attribute. Assigning a new value to an existing attribute is done using setAttribute.
Var myLinkFive =document.getElementById(“Prod_5);myLinkFive.setAttribute(“abc”, ”Awesome”);
document.forms
This method is used to access “form” collections of the DOM structure.
example
<formid=”my_form”method=”post”action=”abc.html”> <inputtype=”checkbox”value=”one”name=”options”id=”option1”checked=”checked” />One </br> <inputtype=”checkbox”value=”two”name=”options”id=”option2” /> Two </br> <inputtype=”checkbox”value=”three”name=”options”id=”option3” /> Three </br></form>
Let’s find out the “Checked” state:
var myCheckBoxOne =document.forms[“my_form”][“option1”];alert(myCheckBoxOne.checked);
innerHTML
It’s supported on mostly every browser or platform.
Var myContentHolder =document.getElementById(“abc”);myContentHolder.innerHTML = “<p> These are the most essential DOM methods in JavaScript</p>”;