Getting the value of text from a text object in JavaScript

Viewed 49701

I have an HTML file which contains the following line:

<div><img src="img1.gif"><br>My Text</div>

I'm trying to select the phrase "My Text" using JavaScript so I can change it. I'm able to get the object that contains the text by using lastChild, but can't figure out how to get the value of the text itself.

6 Answers

you can access text content of the [object Text] by the "data" property. this property is R/W so that you can use it to read text as well as to write it.

<div><img id="my_img_1" src="img1.gif">My Text</div>
<script>
    alert('my current text = ' + lastChild.data);
    lastChild.data = 'new text';
</script>

you can find more here

Related