MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaLang DesignEmacsUnicode ♥

JavaScript: Get a Element's {Type, Name, Value}

,

This page shows you how to get a HTML/XML element's {type, name, value}.

Node Type

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:

meaningreturn value
ELEMENT_NODE1
ATTRIBUTE_NODE2
TEXT_NODE3
CDATA_SECTION_NODE4
ENTITY_REFERENCE_NODE5
ENTITY_NODE6
PROCESSING_INSTRUCTION_NODE7
COMMENT_NODE8
DOCUMENT_NODE9
DOCUMENT_TYPE_NODE10
DOCUMENT_FRAGMENT_NODE11
NOTATION_NODE12

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 Name

‹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.

Node Value

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.

blog comments powered by Disqus