DOM (Document Object Model)

In this post, we will discuss Document Object Model(DOM) along with its properties and methods used to manipulate Document.

Introduction:
The Document Object Model (DOM) is a programming interface for HTML and XML(Extensible markup language) documents. It defines the logical structure of documents and the way a document is accessed and manipulated.

Note: It is called as a Logical structure because DOM doesn’t specify any relationship between objects.

DOM is a way to represent the webpage in the structured hierarchical way so that it will become easier for programmers and users to glide through the document. With DOM, we can easily access and manipulate tags, IDs, classes, Attributes or Elements using commands or methods provided by Document object.

Structure of DOM:



DOM can be thought of as Tree or Forest(more than one tree). The term structure model is sometimes used to describe the tree-like representation of a document. One important property of DOM structure models is structural isomorphism: if any two DOM implementations are used to create a representation of the same document, they will create the same structure model, with precisely the same objects and relationships.

Why called as Object Model ?

Documents are modeled using objects, and the model includes not only the structure of a document but also the behavior of a document and the objects of which it is composed of like tag elements with attributes in HTML.

Properties of DOM:

Let’s see the properties of document object that can be accessed and modified by the document object.

  1. Window Object: Window Object is at always at top of hierarchy.
  2. Document object: When HTML document is loaded into a window, it becomes a document object.
  3. Form Object: It is represented by form tags.
  4. Link Objects: It is represented by link tags.
  5. Anchor Objects: It is represented by a href tags.
  6. Form Control Elements:: Form can have many control elements such as text fields, buttons, radio buttons, and checkboxes, etc.

Methods of Document Object:

  1. write(“string”): writes the given string on the document.
  2. getElementById(): returns the element having the given id value.
  3. getElementsByName(): returns all the elements having the given name value.
  4. getElementsByTagName(): returns all the elements having the given tag name.
  5. getElementsByClassName(): returns all the elements having the given class name.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<Table>
   <ROWS>
      <TR>
         <TD>Car</TD>
         <TD>Scooter</TD>
      </TR>
      <TR>
         <TD>MotorBike</TD>
         <TD>Bus</TD>
      </TR>
   </ROWS>
</Table>

chevron_right


What DOM is not ?

  1. The Document Object Model is not a binary description where it does not define any binary source code in its interfaces.
  2. The Document Object Model is not used to describe objects in XML or HTML whereas the DOM describes XML and HTML documents as objects.
  3. The Document Object Model is not represented by set of data structures; it is an interface which specifies object representation.
  4. The Document Object Model does not show criticality of objects in documents i.e it doesn’t have information about which object in document is appropriate to the context and which is not.

Levels of DOM:

  1. Level 0: Provides low-level set of interfaces.
  2. Level 1: DOM level 1 can be described in two parts: CORE and HTML.
    • CORE provides a low-level interfaces that can be used to represent any structured document.
    • HTML provides high-level interfaces that can be used to represent HTML document.
  3. Level 2 : consists of six specifications: CORE2, VIEWS, EVENTS, STYLE, TRAVERSAL and RANGE.
    • CORE2: extends functionality of CORE specified by DOM level 1.
    • VIEWS: views allows programs to dynamically access and manipulate content of document.
    • EVENTS: Events are scripts that is either executed by browser when user reacts to web page.
    • STYLE: allows programs to dynamically access and manipulate content of style sheets.
    • TRAVERSAL: allows programs to dynamically traverse the document.
    • RANGE: allows programs to dynamically identify a range of content in document.
  4. Level 3: consists of five different specifications: CORE3, LOAD and SAVE, VALIDATION, EVENTS, and XPATH.
    • CORE3: extends functionality of CORE specified by DOM level 2.
    • LOAD and SAVE: allows program to dynamically load the content of XML document into DOM document and save the DOM Document into XML document by serialization.
    • VALIDATION: allows program to dynamically update the content and structure of document while ensuring document remains valid.
    • EVENTS: extends functionality of Events specified by DOM Level 2.
    • XPATH: XPATH is a path language that can be used to access DOM tree.

References:

  1. https://codescracker.com/js/js-dom-levels.htm
  2. https://www.w3.org/TR/DOM-Level-3-Core/introduction.html


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.