Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use jQuery to select text not in an element

Tags:

jquery

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?

like image 205
benhowdle89 Avatar asked Jun 17 '11 16:06

benhowdle89


1 Answers

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

How do I select text nodes with jQuery?

http://jsfiddle.net/6us8r/

like image 97
js1568 Avatar answered Nov 15 '22 16:11

js1568