I know, this is really a noob question and I don't really know what went wrong.
The code is very simple. I have a web page that contains the following:
<div id="mydiv">
<p>first</p>
</div>
The <p> tags are created by the following js code:
paragraph = document.createElement("p");
text = document.createTextNode("first");
paragraph.appendChild(text);
document.getElementById('mydiv').appendChild(paragraph);
I want to use javascript to extract the text from the first <p> tag. In my code, I wrote:
var mydiv = document.getElementById('mydiv');
var p = mydiv.childNodes[0];
var text = p.childNodes[0]; // I tried others like p.text
alert(text.nodeValue);
I tried many other ways to get the text, but I just can't get it. Everytime I get null or blank or Object Text or no pop up at all. I am pretty sure document.getElementById('mydiv') is getting the right div. Can someone please give me an example, please, I feel so stupid because this shouldn't be a problem but I just can't really find a solution. Thank you so much!