Skip to content Skip to sidebar Skip to footer

Dom/javascript: Get Text After Tag

How do I get the text 'there' that comes after a tag in an html document:

hellothere

I see that there is a way to do it with xpath: Get text

Solution 1:

Something like this?

var p = document.getElementsByTagName("p")[0];
alert(p.childNodes[1].textContent)

Solution 2:

use this .. http://jsfiddle.net/2Dxy4/

This is your HTML --

<pid="there"><a>hello</a>
   there
</p>

In your JS

alert(document.getElementById("there").lastChild.textContent)​

or

alert(document.getElementById("there").childNodes[1].textContent)​

Solution 3:

You can use lastChild and textContent to get it: http://jsfiddle.net/M3vjR/

Solution 4:

Well if you use jQuery there is a sneaky way to get this

x = $("<p><a>hello</a>there</p>")x.contents()[1]

Solution 5:

You can find children tags of your parent tag:

var pTag = ...
var childNodes = pTag.childNodes;

Finally you can find your text in a text node after a node.

Post a Comment for "Dom/javascript: Get Text After Tag"