Search w3schools.com:

SHARE THIS PAGE

HTML DOM Properties


Properties are values of nodes (HTML Elements) that you can get or set


Programming Interface

The HTML DOM can be accessed with JavaScript (and other programming languages).

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

A method is an action you can do (like add or delete a node).

A property is a value that you can get or set (like the name or content of a node).


The innerHTML Property

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

The innerHTML property is useful for getting or replacing the content of HTML elements.

Example

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

Example

<html>
<body>

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

<script>
var txt=document.getElementById("intro").innerHTML;
document.write(txt);
</script>

</body>
</html>

Try it yourself »

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

lamp The innerHTML property can be used to get or change any HTML elements, including <html> and <body>.


The nodeName Property

The nodeName property specifies the name of a node.

  • nodeName is read-only
  • nodeName of an element node is the same as the tag name
  • nodeName of an attribute node is the attribute name
  • nodeName of a text node is always #text
  • nodeName of the document node is always #document

Note: nodeName always contains the uppercase tag name of an HTML element.


The nodeValue Property

The nodeValue property specifies the value of a node.

  • nodeValue for element nodes is undefined
  • nodeValue for text nodes is the text itself
  • nodeValue for attribute nodes is the attribute value

Get the Value of an Element

The following example retrieves the text node value of the <p id="intro"> tag:

Example

<html>
<body>

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

<script type="text/javascript">
x=document.getElementById("intro");
document.write(x.firstChild.nodeValue);
</script>

</body>
</html>

Try it yourself »


The nodeType Property

The nodeType property returns the type of node. nodeType is read only.

The most important node types are:

Element type NodeType
Element 1
Attribute 2
Text 3
Comment 8
Document 9



Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]