1

It must be late... Given the following HTML, how would one select all of the paragraphs except for the first paragraph inside each of the div.thePosts?

I have tried:

$('.thePost').children('p:gt(0)')

and

$('.thePost p:gt(0)')

and

$('.thePost > p:gt(0)')

All of which work fine for the very first div.thePost but end up selecting all other <p> tags within any other div with the class of thePost.

<div id="contentmiddle">

<div class="thePost">

  <h1>...</h1>

  <h3>...</h3>

  <span>...</span> 

  <p>...</p>
  <p>...</p>
  <p>...</p>
  <p>...</p>

</div><!-- /thePost -->

<div class="thePost">

  <h1>...</h1>

  <h3>...</h3>

  <span>...</span> 

  <p>...</p>
  <p>...</p>
  <p>...</p>
  <p>...</p>

</div><!-- /thePost -->

</div>​<!-- /contentmiddle -->
jonathanbell
  • 2,507
  • 5
  • 23
  • 40

5 Answers5

6

:first-of-type is not a jQuery selector, so unless a browser supports it natively in CSS, the solutions using :first-of-type won't work.

If you need to support older browsers (IE < 9), you need to use next siblings selector ~ instead:

$('.thePost > p ~ p')
Community
  • 1
  • 1
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
4

How about $('.thePost > p:not(:first-of-type)')

Musa
  • 96,336
  • 17
  • 118
  • 137
2

Try this

$('.thePost > p:not(:first)')

OR

$('.thePost').find('p:gt(0)')
Sushanth --
  • 55,259
  • 9
  • 66
  • 105
1

$('.thePost p:not(:first-of-type)')

tomaroo
  • 2,524
  • 1
  • 19
  • 22
0

you can try too $('p:gt(0)','.thepost'));

Loïc bcn
  • 154
  • 5