Get the text without HTML tags by javascript

Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
Marius
Posts: 60

Get the text without HTML tags by javascript

Hi,
I have a Div with some content and other html tags inside.
How can I get all the text content of this Div, without the html tags?
I ussed innerHTML property, but gets the tags too.
This is what i tried:

Code: Select all

<div id="d_id">
  Some content ...
  <p>Other html element.</p>
  <span>Text in Another html tag</span>
</div>
<script>
var get_text = document.getElementById('d_id').innerHTML;
alert(get_text);
</script>

Admin
You can use this:

Code: Select all

<div id="d_id">
  Some content ...
  <p>Other html element.</p>
  <span>Text in Another html tag</span>
</div>
<script>
var get_elm = document.getElementById('d_id');

// innerText for IE, textContent for other browsers
var get_text = get_elm.innerText || get_elm.textContent;

alert(get_text);
</script>
Or, if you use jQuery:

Code: Select all

var get_text = $('#d_id').text();

Similar Topics