0
   <td>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
        <p></p>
    </td>

I want to get all ps except the first 3.

I am doing it like so

td p:not(:nth-child(1),:nth-child(2),:nth-child(3))

But what if I want all ps except first 20? Will I have to write manually like I did above?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Umair Ayub
  • 19,358
  • 14
  • 72
  • 146
  • 1
    Is that number dynamic or static? In other words are you looking for a CSS selector to be used without jQuery or with jQuery? – Harry Feb 28 '15 at 13:43
  • 1
    Note that what you currently have only works in jQuery and is not currently a valid CSS selector. See [Why is my jQuery :not() selector not working in CSS?](http://stackoverflow.com/questions/10711730/why-is-my-jquery-not-selector-not-working-in-css) – BoltClock Feb 28 '15 at 14:28

4 Answers4

6

This should work.

td p:nth-child(n+5){
  color: red;
}
  <table>
    <tr>
      <td>
        <p>1</p>
        <p>2</p>
        <p>3</p>
        <p>4</p>
        <p>5</p>
        <p>6</p>
        <p>7</p>
    </td>
      </tr>
    </table>

Thus:

td p:nth-child(n+21)

to select all p's except the first 20s.

Heri Hehe Setiawan
  • 1,535
  • 1
  • 12
  • 19
  • 1
    +1 Although the question is open to jQuery answers, using standard selectors that are supported is preferred as not only can they be used in CSS, but in a script they can also leverage the browser's native selector engine instead of jQuery's proprietary engine. – BoltClock Feb 28 '15 at 14:24
2

Easier to use :gt(index) selector. This will collect matching elements with index greater than selector index

$('td p:gt(19)');

Note that index passed is zero based so for more than 20 elements use index 19

Reference: :gt() selector docs

charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

Try

var p = $("td p").slice(20);

See jQuery.fn.slice

$(function() {
for (var i = 0; i < 30; ++i) {
  $("table td").append("<p>" + i)
}

var p = $("td p").slice(20);
p.html(function(i, html) {
  return html.replace(/.*/, Math.random())
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<td></td>
</table>
guest271314
  • 1
  • 15
  • 104
  • 177
-3

If you want to get all p except the first 3 :

$("td p:gt(2)");