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://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://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 attribute can be used to add CSS styles directly in the HTML tag?
type style class
<div style="width: 80%; border: 3px solid #888888;">Content</div>
Which CSS transform method distorts the HTML element in a given angle (including its content)?
translate() scale() skew()
#some_id {
  transform: skew(20deg, 25deg);
  -ms-transform: skew(20deg, 25deg);   /* IE 9 */
  -webkit-transform: skew(20deg, 25deg);   /* Safari and Chrome */
}
Click on the function which converts a Date object to a string.
indexOf() toString() getDate()
var rightnow = new Date();
alert( rightnow.toString() );
Which function applies a callback function to the elements of the given array?
array_merge() array_search() array_map()
$arr = arra("abc", "<p>xyz</p>", "<em>PHP</em>");
// apply the strip_tags() function to delete HTML tags from each array item
$arr = array_map("strip_tags", $arr);
}
Document Object - DOM

Last accessed pages

  1. SHA512 Encrypt hash in JavaScript (7776)
  2. Mahjong2 (855)
  3. Mahjong Games (17769)
  4. Get Lower, Higher, and Closest Number (1574)
  5. Mahjong Long (687)

Popular pages this month

  1. Qwop (3030)
  2. Drag Racer V3 (1516)
  3. Bubbles3 (727)
  4. Flash Games - Free online Games (677)
  5. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (665)