The document object is an object that is created by the browser for each HTML page (document) that is viewed.
The Document object is a property of the Window object, and can be accessed through the window.document, but, becouse the "window" is the main object, it can be omited.
DOM stands for "Document Object Model", it is a hierarchy of data types for HTML documents, links, forms, tags, and everything else that can be represented in HTML code.
DOM allows JavaScript to access the structure of the document in the browser.

Document Object Properties


- Exemples with some of these properties:
1) Display and change the text from TITLE meta-tag
<script type="text/javascript"><!--
document.write(document.title);         // Document Object - DOM
document.title = 'New title';         // "New title" will appear in the tittle bar
--></script>

2) In the following example, the JS script gets and writes the domain and the complete URL of the page.
<script type="text/javascript"><!--
var domain_site = document.domain;
var url = document.URL;

document.write(domain_site+ '<br />');         // coursesweb.net
document.write(url);         // http://coursesweb.net/javascript/document-object-dom
--></script>

3) In the next example three link tags are defined and a JS script writes the address from the second link.
<a href="http://coursesweb.net" title="Courses Web">CoursesWeb</a><br />
<a href="http://www.marplo.net" title="Lessons, Tutorials">Lessons, Tutorials</a><br />
<a href="http://www.yahoo.com" title="yahoo.com">yahoo.com</a><br />
<script type="text/javascript"><!--
var adrs = document.links;      //  gets all links from <a> tags

document.write(adrs[1]);         // http://www.marplo.net/
--></script>

Document Object Methods


• You can get the content of a HTML tag, when you know its ID, by using one of the fallowing sintaxes:
variable_name = document.getElementById("id").innerHTML;
      or
variable_name = document.getElementById("id").childNodes[0].nodeValue;

• To change or add a content inside a HTML that has an ID, use the fallowing expresion:
document.getElementById("id").innerHTML = "content";

- Exemples with some of these methods:
1) The following script displays an alert with the text added in a tag with id="an_id"
<div id="an_id">JS lessons and tutorials</div>
<script type="text/javascript"><!--
var str = document.getElementById('an_id');
alert(str.innerHTML);          // JS lessons and tutorials
--></script>

2) In the next example, when you click on the text "DOM JavaScript lesson", it is called a function that change that content, using the ID of that tag (transfered as a parameter).
- the expresion this.id gets the value of the ID attribute added in own element.
<script type="text/javascript"><!--
function changeTxt(id) {
  document.getElementById(id).innerHTML = "<u>new content</u>";
}
--></script>
<div id="ex2" onclick="changeTxt(this.id)">DOM JavaScript lesson</div>
- It will displays the fallowing rezult, click on the text to see the effect.
DOM JavaScript lesson

3) The next example uses the getElementsByTagName() method and return an alert with the number of LI tags
<script type="text/javascript"><!--
function nrElements(tag) {
  var tags = document.getElementsByTagName(tag);
  alert(tags.length);
}
--></script>
<ul>
 <li>Courses</li>
 <li>Lessons</li>
 <li>tutorials</li>
</ul>
<button onclick="nrElements('li')">How many LI tags</button>
- Click the button to see the result.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
}
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
alert(content);
Click on the "echo" correct instruction.
echo "CoursesWeb.net" echo "CoursesWeb.net"; echo ""CoursesWeb.net";
echo "Address URL: http://CoursesWeb.net";
Document Object - DOM

Last accessed pages

  1. Validate radio and checkbox buttons (4204)
  2. Mystic India Pop (4909)
  3. Butterfly Kyodai (13386)
  4. Bike Mania2 (346)
  5. Auto Moto Games (3272)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (50744)
  2. Read Excel file data in PHP - PhpExcelReader (39506)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (37811)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (34070)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (31525)