neo-nant neo-nant - 1 year ago 152
Javascript Question

Javascript nodeValue returns null

Title should make my problem well described.Here goes my code.

<div id="adiv"><text>Some text</text></div>
<script type="text/javascript">
function vb(){
alert(document.getElementById("adiv").firstChild.nodeValue); //returns null
<input type="button" onclick="vb();" value="get"/>

wheres the problem..?

Answer Source

In order to get [merged] text content of an element node:

function vb(){
var textnode = document.getElementById("adiv").firstChild;
alert(textnode.textContent || textnode.innerText);

In order to get text content of a text node:

function vb(){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download