The document object model(DOM) - AJAX

Let’s take a look at some of the methods and properties that help you access and manipulate this information, often called Walking The DOM.

Nodes
Suppose that our JavaScript object myxmlDoc contains the XML listing of the yacht race.The document element, <race>, contains two elements of type <yacht>; we say it has two children.

In general, you can get the number of children belonging to a particular element by using the childNodes .length property of the object.Because <race> is the document element, it is at the top of the object hierarchy, and we can refer to it simply with the variable myxmlDoc:

We can also determine information about individual children by appending the node number in parentheses:

The preceding line refers to the first <yacht> element appearing in the document.

We can test for the presence of children for a particular element by using the hasChildNodes() method

This line returns true because the second yacht in the document has three children (with tag names name, skipper, and helm). However,

returns false because the <name> element within that <yacht> element has no children.

Getting Tagnames
The tagname property allows you to find the tagname associated with a particular element:

The preceding line returns skipper.

Getting Element Attributes
The method getAttribute(“AttributeName”) can be used to return the attribute values for a given element:

This line returns 74.

Tag Contents
The text property can be used to return the contents of a particular element.The line

would return Walter Jeffries.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

AJAX Topics