jquery gt
jQuery :gt() Selector
The :gt() selector in jQuery is used to select elements with an index greater than a specified number within a matched set. It is part of jQuery's positional selectors and is zero-indexed.
Syntax:
$("selector:gt(index)")
selector: The base selector to filter.index: The index after which elements are selected (exclusive). Negative indices count from the end.
Key Features
- Zero-Based Indexing: The count starts at 0. For example,
:gt(1)selects elements at index 2 and beyond. - Negative Indices: A negative index counts backward from the end. For example,
:gt(-3)selects elements after the third-to-last element. - Filtering: Often used with other selectors to narrow down results, such as
$("tr:gt(0)")to skip the first row in a table.
Examples
-
Selecting Rows After the First Two:
$("tr:gt(1)").css("background-color", "yellow");This targets all
<tr>elements with an index greater than 1 (i.e., third row onward). -
Using Negative Index:
$("li:gt(-4)").addClass("highlight");This selects all
<li>elements except the last three. -
Combining with Other Selectors:
$("ul.items li:gt(2)").hide();Hides all
<li>elements beyond the third one withinul.items.
Notes
- The
:gt()selector is deprecated in jQuery 3.4.0. Alternatives include.slice()or.filter():$("tr").slice(2).css("background-color", "yellow"); - Unlike
:gt(),.slice()includes the starting index, so adjust indices accordingly.
Browser Compatibility
Works across all major browsers, but avoid using deprecated selectors in newer jQuery versions for future-proofing.






