hellothere

I ...">

DOM / Javascript: get text after tag

How to get the text "there" that appears after the tag in the html document:

<p><a>hello</a>there</p> 

I see that there is a way to do this with xpath:

Get text from next tag

but I do not use xpath, and I hope you do not have to start just for that. I understand that I can get ALL the text inside the p tag, but I want to get only the text "there", and also find out its connection with p and the tags. It does not seem to be a child or a brother. (It can be assumed that I can get any of the other elements / nodes so that it can be relative to this.) It seems that every DOM tutorial ignores the fact that text can occur outside of tags.

Thanks.

+7
source share
5 answers

Something like this ?

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

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

This is your HTML -

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

In your js

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

or

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

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

0
source

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

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

0
source

You must first get the innerhtml attribute, then take the resulting innerhtml using a substring, you can have your part.

 $(document).ready(function () { var getA=$("p >a").text(); var getA=getA.length; var takeTotal=$("p").text(); var result=takeTotal.substring(get); alert(result); }); 
-one
source

All Articles