0

Possible Duplicate:
Getting the contents of an element WITHOUT its children

How can I get foobar out of the following HTML with jquery/javascript, without using regular expressions?

<div id="somediv">
foobar
<span></span>
</div>
Community
  • 1
  • 1
Sonnenhut
  • 4,073
  • 5
  • 21
  • 21

4 Answers4

2

Just use .text().

$("#somediv").text();

Or, if the span has content which you want to ignore, use:

$("#somediv").contents().first().text();
Jack Murdoch
  • 2,864
  • 1
  • 18
  • 27
1

If you just want the text from the first child node, use

$("#somediv").contents().eq(0).text()
Dogbert
  • 212,659
  • 41
  • 396
  • 397
0

Try this:

var text = document.getElementById("somediv").getElementsByTagName("span")[0].innerHTML;
The Mask
  • 17,007
  • 37
  • 111
  • 185
0

You can do the following:

$('#somediv').text().trim();

The trim will get rid of the extra whitespace from the carriage returns and leave you with just foobar.

rahlstrom
  • 699
  • 1
  • 5
  • 9