9

Say i have this:

<div class='myDiv'>
    <p>hello</p>
    hello
    <p>Hello</p>
</div>

How does one grab the text hello between the two P tags using jQuery?

benhowdle89
  • 36,900
  • 69
  • 202
  • 331

2 Answers2

15
$('.myDiv')
  .contents()
  .filter(function() {
    return this.nodeType == Node.TEXT_NODE;
  }).text();

How do I select text nodes with jQuery?

http://jsfiddle.net/6us8r/

Community
  • 1
  • 1
js1568
  • 7,012
  • 2
  • 27
  • 47
0

js1568 has a better approach

$('div.myDiv').filter('p').text() would probably work.

I take it back, filter would not work. maybe something like:

var jText = $('div.myDiv').clone();
jText.find('p').remove();
jText.text();
aepheus
  • 7,827
  • 7
  • 36
  • 51