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:
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.
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) Well, if you use jQuery, there is a hidden way to get this
x = $("<p><a>hello</a>there</p>")x.contents()[1]
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); });