0
<p id="mydiv">
<img alt="Assets Received" src="/images/icons/statuses/generic.png" title="" height="16" width="16"> I want to get this text only, not the image element before me
</p>

I want to retrieve the text inside of mydiv excluding the image tag. fiddle https://jsfiddle.net/Town/5L3rd/

Dinǝsh Gupta
  • 377
  • 1
  • 2
  • 9
  • If you, like in this case, always have the text being the last node you could always use; `alert(document.getElementById('mydiv').lastChild);` Works on IE 6 and up. – dojs Mar 12 '15 at 17:05

1 Answers1

1

In modern browsers this property is called textContent:

document.getElementById('mydiv').textContent // " I want to get this text only, not the image element before me"

If you want to support rocks and dust (IE8), you also need to check the innerText property. Together you can create a function to anonymously get the text:

function getText(el){
    return el.textContent || el.innerText;
}
Eric
  • 18,532
  • 2
  • 34
  • 39