Document Object Model (DOM)

Document Object Model (DOM) The Document Object Model is a storing of the HTML elements and their styles in a tree with the root of the tree being the document object which represents the <html> element.

"document.body" represents the <body> element. The window object is an implicit object which represents the tab opened in the browser.

For example, "window.document.body" is the same as "document.body";

To access an element by the "id" attribute, use the
document.getElementById("<id of the element>")

To access elements by the "class" attribute, use the
document.getElementsByClassName("<class name of the element(s)>");

To redirect a browser to another web page, use the
document.location.href= "http://wwww.example.com"
To refresh a web page, use
document.location.reload();

To access all the forms in the document,use
document.forms object
For instance,
document.forms[0] will be the first form in the document tree. The document tree is parsed in Depth-First Search manner

To set an Attribute in an HTML element,
document.getElementById("id").setAttribute("class","red");

To get an attribute in an HTML element,
document.getElementById("id").getAttribute("class");

To access a textfield whose name attribute is "textfield1" in the first form of the document as parsed in the document by The DOM in Depth First Search, use

document.forms[0].textfield1

To access the value of the "textfield1" use
document.forms[0].textfield1.value

To set the value of the "textfield1" use
document.forms[0].textfield1.value="Hello"
To change the HTML of an element, use
document.getElementById("id").innerHTML="<h />"

To store session information across nultiple requests, use
document.cookie ="Name1: value1; Name2: value2";

To check if an element is existing, use
if(type of document.getElemenentById("hello") != "undefined")

To create an HTML element, use
document.createElement("h1");

To Append a child element to a poarent HTML eleement, use
document.body.appendChild("<html_text>");

Additional topics

  • To get the first HTML element has satifies the query selector use
    document.querySelector("#demo");
  • To get all the HTML elements that satisify the query selector use
    document.querySelectorAll(".demo");
  • To change the class or to add a class use
    document.body.className = " hide";
    document.body.className += " hide";

Comments

Popular posts from this blog

Parallel Database design, query processing

Laravel | PHP | Basics | Part 2

Apache Hadoop | Running MapReduce Jobs