jQuery API

:gt() Selector

gt selector

version added: 1.0jQuery(':gt(index)')

  • index
    Zero-based index.

Description: Select all elements at an index greater than index within the matched set.

index-related selectors

The index-related selector expressions (including this "greater than" selector) filter the set of elements that have matched the expressions that precede them. They narrow the set down based on the order of the elements within this matched set. For example, if elements are first selected with a class selector (.myclass) and four elements are returned, these elements are given indices 0 through 3 for the purposes of these selectors.

Note that since JavaScript arrays use 0-based indexing, these selectors reflect that fact. This is why $('.myclass:gt(1)') selects elements after the second element in the document with the class myclass, rather than after the first. In contrast, :nth-child(n) uses 1-based indexing to conform to the CSS specification.

Additional Notes:

  • Because :gt() is a jQuery extension and not part of the CSS specification, queries using :gt() cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use $("your-pure-css-selector").slice(index) instead.

Example:

Finds TD #5 and higher. Reminder: the indexing starts at 0.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <table border="1">

    <tr><td>TD #0</td><td>TD #1</td><td>TD #2</td></tr>
    <tr><td>TD #3</td><td>TD #4</td><td>TD #5</td></tr>

    <tr><td>TD #6</td><td>TD #7</td><td>TD #8</td></tr>
  </table>
<script>$("td:gt(4)").css("text-decoration", "line-through");</script>

</body>
</html>

Demo:

Support and Contributions

Need help with :gt() Selector or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to :gt() Selector? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • http://twitter.com/jeremyboyd Jeremy Boyd

    While working on a pagination plugin, I came across something that I had to workaround.
    “:gt(-1)”

    Here is my work around:

    var currentPage = -1, pageSize = 5;
    function nextPage() {
    ++currentPage;
    $(“.s”).hide();
    var index = currentPage * pageSize – 1;
    if (index < 0) { $(“.savedStationery:eq(0)”).show(); }
    $(“.s:gt(” + (index >= 0 ? index : 0) + “)”).show();
    $(“.s:gt(” + (index + pageSize) + “)”).hide();
    }
    nextPage();

    • Pedro

      I just ran into this, too. Seems like an oversight when the selector was created (though, I'm happy to hear arguments otherwise). Thanks for the workaround, Jeremy.

    • http://waldin.net/ Ray

      I'm having a similar problem, wanting to always select all elements of someClass greater than or equal to position N using :gt(N-1), and came up with this succinct workaround:

      $(“.someClass”).filter(“:eq(“+ N + “), :gt(” + N + “)”)

  • Jorenm

    I feel they should add GTE and LTE, but for now I find slice is the most reliable way to get a range of elements.

  • Jorenm

    I feel they should add GTE and LTE, but for now I find slice is the most reliable way to get a range of elements.

  • Valid

    “I'm having a similar problem, wanting to always select all elements of someClass greater than or equal to position N using :gt(N-1), and came up with this succinct workaround:

    $(“.someClass”).filter(“:eq(“+ N + “), :gt(” + N + “)”)”

    This works, however, the shorthand does not work
    $(“.someClass:eq(“+ N + “), :gt(” + N + “)”)”

  • gowri

    How to select grater than index value

    i want to select TD #0 to TD #4 columns in demo

    any one can help me !

    • Arthberzieri

      why don't you use :lt() ?