Get text inside parent div

I am trying to get text from an element that is not the first parent of the string. i.e.

<div id="wrp">
    <h1>
        <a href="www.something.com">this is the text I want</a>
    </h1>
</div>

let's say I got the parent div element=document.getElementById("wrap"), and now I want to get its final text without looking at its children: h1and a. just the text that I see on the site.

Is it possible? Please show me an example.

+4
source share
2 answers

You can use both innerText, and textContent, but innerText does not work in Firefox . So, the solution of the crossbrowser:

var text = element.innerText || element.textContent;

JSBin . An interesting comparison about them .

+1

, anchor , div div ( , div , ):

var wrap = document.getElementById("wrap");
var text = wrap.getElementsByTagName("a")[0].innerHTML;

Mozilla, , . textContent, , , , , :

var wrap = document.getElementById("wrap");
var text = document.getElementById("wrap").textContent;
0

All Articles