This page shows you how to get a HTML/XML element's {type, name, value}.
Use ‹node›.nodeType to get a element's type. The ‹node› is a HTML element object.
The return value is a number. Usually 1 or 3. “1” means it's a HTML/XML element. “3” means its content.
Here's a complete list of return values:
| meaning | return value |
|---|---|
| ELEMENT_NODE | 1 |
| ATTRIBUTE_NODE | 2 |
| TEXT_NODE | 3 |
| CDATA_SECTION_NODE | 4 |
| ENTITY_REFERENCE_NODE | 5 |
| ENTITY_NODE | 6 |
| PROCESSING_INSTRUCTION_NODE | 7 |
| COMMENT_NODE | 8 |
| DOCUMENT_NODE | 9 |
| DOCUMENT_TYPE_NODE | 10 |
| DOCUMENT_FRAGMENT_NODE | 11 |
| NOTATION_NODE | 12 |
Example:
var xx = document.getElementById("id63656"); console.log("type is:" + xx.nodeType); console.log("firstChild type is:" + xx.firstChild.nodeType);
As of , this works in all major browsers.
‹node›.nodeName returns the node's (tag) name (as string), if the node is a element node (nodeType returns 1). If it's a text node, the value is "#text".
var xx = document.getElementById("id43160"); console.log("name is:" + xx.nodeName); console.log("firstChild name is:" + xx.firstChild.nodeName);
As of , this works in all major browsers.
Use ‹node›.nodeValue to get the content of text node. For most other node types, it returns null.
var xx = document.getElementById("id82521"); console.log("node value is:" + xx.nodeValue); console.log("firstChild node value is:" + xx.firstChild.nodeValue);
To change a HTML element's content, you can just set “nodeValue” to some text. See: JavaScript: Changing HTML Content Example.
As of , this works in all major browsers.