W3Schools.com


  
HOME HTML CSS XML JAVASCRIPT ASP PHP SQL MORE... REFERENCES | EXAMPLES | FORUM | ABOUT

HTML DOM Properties and Methods

« Previous Next Chapter »

Properties and methods define the programming interface of the HTML DOM.


Programming Interface

In the DOM, HTML documents consist of a set of node objects. The nodes can be accessed with JavaScript or other programming languages. In this tutorial we will use JavaScript.

The programming interface of the DOM is defined by standard properties and methods.

Properties are often referred to as something that is (i.e. the name of a node).

Methods are often referred to as something that is done (i.e. remove a node).


HTML DOM Properties

Some DOM properties:

  • x.innerHTML - the text value of x
  • x.nodeName - the name of x
  • x.nodeValue - the value of x
  • x.parentNode - the parent node of x
  • x.childNodes - the child nodes of x
  • x.attributes - the attributes nodes of x

Note: In the list above, x is a node object (HTML element).


HTML DOM Methods

Some DOM methods:

  • x.getElementById(id) - get the element with a specified id
  • x.getElementsByTagName(name) - get all elements with a specified tag name
  • x.appendChild(node) - insert a child node to x
  • x.removeChild(node) - remove a child node from x

Note: In the list above, x is a node object (HTML element).


The innerHTML Property

The easiest way to get or modify the content of an element is by using the innerHTML property.

innerHTML is not a part of the W3C DOM specification. However, it is supported by all major browsers.

The innerHTML property is useful for returning or replacing the content of HTML elements (including <html> and <body>), it can also be used to view the source of a page that has been dynamically modified.

Example

The following code to gets the innerHTML (text) from the <p> element with id="intro":

Example

<html>
<body>

<p id="intro">Hello World!</p>

<script type="text/javascript">
txt=document.getElementById("intro").innerHTML;
document.write("<p>The text from the intro paragraph: " + txt + "</p>");
</script>

</body>
</html>

Try it yourself »

In the example above, getElementById is a method, while innerHTML is a property.


childNodes and nodeValue

We can also use the childNodes and NodeValue properties to get the content of an element.

The following code to gets the value of the <p> element with id="intro":

Example

<html>
<body>

<p id="intro">Hello World!</p>

<script type="text/javascript">
txt=document.getElementById("intro").childNodes[0].nodeValue;
document.write("<p>The text from the intro paragraph: " + txt + "</p>");
</script>

</body>
</html>

Try it yourself »

In the example above, getElementById is a method, while childNodes and nodeValue are properties.

In this tutorial we will mostly use the innerHTML property. However, learning the method above is useful for understanding the tree structure of the DOM and dealing with XML files.


« Previous Next Chapter »

Free Online Website Builder - No Downloading Needed

Create a free Flash website with our simple, online web design editing platform. Stunning templates and user-friendly tools make website building easy and fun.

Start Creating your free website now!


Altova® MissionKit® - Integrated Suite of XML Tools

Altova MissionKit

The Altova MissionKit is an integrated suite of tools ideal for:

  • XML development
  • Web & Web services development
  • Data mapping & integration
  • Rendering & publishing XML & database data
  • XBRL validation, taxonomy editing, transformation & rendering
  • Chart & report generation for XML & XBRL

The MissionKit for XML Developers includes XMLSpy® - the industry-leading XML editor; MapForce® - a graphical data mapping, conversion, and integration tool; StyleVision® - a visual XSLT stylesheet and report designer; DiffDog® - an XML-aware diff/merge tool; and 2 additional tools.

Try all 6 products free for 30 days!

Download a fully-functional free trial

  Altova Missionkit

 

WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
Top Web Hosting
$1 Domain Sale
WEB BUILDING
Download XML Editor
FREE Flash Website
6,000+ HTML Templates
Download now (FREE)
Advertise on Google
Stand out on Google search and maps Click Here
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE