From WikiEducator
Jump to: navigation, search


VmvIcon Objective.png

: Understand the DOM

By the end of this page you will be able to:

  • Understand the Document Object Model

In order for a computer to understand a document, it needs to follow a pre-defined structure built up of identifiable objects (document objects).

On a computer the term document is used to represent many file types, indeed we use folders to arrange computer documents (files?) including Word-processing (from MS-Word or Open Office), Spreadsheets, to databases and multimedia files (such as images, sounds and videos).

Kantor (2006)[1] identified 3 aspects that define a document:

  • content
  • structure, and
  • style.
aspectText documentHTML document
contentthe information, or
meta-information(properties e.g. author, table of contents, etc.)
information: content in the <body>,
meta:contained in the <head> tag
structurechapters, paragraphs, listsFormatting tags such as: <p>,<div>, <li>
styleincludes the fonts, colours, etc. At top level can identify the output media, screen, printer, Braille,
<link rel="stylesheet" ... media="print"> (w3c:Media types, n.d.)[2]
to identifying how to display fonts, colours, backgrounds, etc. usually via the style attribute.

Not all documents will contain all three, for example a file containing a list of names and addresses to be used by another computer could have no style information. However, any document to be used for human interaction or display will have all three.

Document tree

The DOM can be thought of as a tree diagram constructed from the tags in an HTML page.

  <title>Simple DOM example</title>
  <script type="text/javascript">
    function fnDisplayName() {
      strMessage=strMessage +" " +document.myForm.txtLname.value;
      alert("Hello " + strMessage);
  <h1>Name Form</h1>
  <form name="myForm">
    <p>Enter Your First Name:</p>
    <input type="text" name="txtFname" value="" />
    <input type="button" value="Press Me" onClick="fnDisplayName()" /></p>
    <div id="Msg"></div>

So a simple DOM for the web page is:

|  |-<title>
|  '-<script>

If you look closely at the example you can see that a form element can be referenced by navigating the DOM tree (document can be related to the body tag).


Another one you have come across is the document.write command where a line of text (usually html code) is inserted into the web page.


In practice in Javascript we normally use getElementByID and provide the elements with an id attribute. So in the example above we can replace

alert("Hello " + strMessage);


document.getElementById('Msg').innerHTML = "Hello " + strMessage;

and the div tag contents will change on the form.

<title>getElementById Example</title>
.displayInfo 	{
       width: 300px; height: 50px; border: 1px solid #808080;
       margin-right:auto;margin-left:auto; background-color: #F6F9ED;
       font-weight: bold; cursor: pointer;}
<script type="text/javascript">
function fnX(){
  document.getElementById('ShowInfo').innerHTML = "YYY";
<div id="ShowInfo" class = "displayInfo" onClick="fnX();">XX</div>


VmvIcon References.png References

  1. Kantor, P. L. (2006). Peter L. Kantor - Lectures and References. Retrieved June 14, 2009 from
  2. w3c:Media types (n.d.) Media types. Retrieved June 14, 2009 from

virtualMV  |  Superquick wiki guide  |  Please give me some feedback

VirtualMV/JavaScript/DocumentObject/Home. (2022). In WikiEducator/VirtualMV wiki. Retrieved August 12, 2022, from http:    (zotero)