Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS get value of generated textnode

I have this Javascript in a for loop:

renderAElements[i] = document.createElement ("a");
        renderAElements[i].setAttribute("href", "#");
        renderAElements[i].setAttribute("class", "expander");
        renderAElements[i].appendChild(expand);

        alert (renderAElements[i].nodeValue);

where expand is created as:

var expand = document.createTextNode("+");

The alert, which is meant to return the link text of each created element returns null. Why is this?

like image 869
YsoL8 Avatar asked Jul 01 '11 10:07

YsoL8


1 Answers

Because you are trying to get the nodeValue of the Element node and not the Text node.

alert (renderAElements[i].firstChild.nodeValue);
like image 196
Quentin Avatar answered Sep 30 '22 14:09

Quentin