Search code examples
javascripthtmlxmlnullnodevalue

Output is null, when getting the nodeValue


I'm getting the value of a node by this code in javascript,

function show(){
        var x = document.getElementsByTagName("allowance")[0];
        var y = x.nodeValue;
        alert(y);
    }

from this xml in html.

<xml style="display: none">
        <students id="lul">
            <student>
                <name>Mark Fajardo</name>
                <allowance>9999</allowance>
            </student>
            <student>
                <name>Rencie Macale</name>
                <allowance>20</allowance>
            </student>
        </students>
    </xml>

But the output that alert projects is just null. Help


Solution

  • Also you can use innerHTML like this

    y = document.getElementsByTagName("allowance")[0].innerHTML;
    alert(y);