jQuery Selector :lt()

Introduction

The :lt() selector selects elements with an index number less than a specified number.

The index numbers start at 0.

$(":lt(index)")
Parameter
Optional
Description
index

Required.

which element to select.
Elements with an index lesser than the specified number is selected.

Select the 4 first <tr> elements:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("tr:lt(4)").css("background-color", "yellow");
});//from w w  w .  j  a  va2 s . c om
</script>
</head>
<body>

<h1>Welcome to My Web Page</h1>

<table border="1">
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>CSS</td>
    <td>Java</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>A</td>
    <td>V</td>
    <td>Sweden</td>
  </tr>
  <tr>
    <td>Javascript</td>
    <td>Python</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>C++</td>
    <td>Q</td>
    <td>XYZ</td>
  </tr>
  <tr>
    <td>Ruby</td>
    <td>W</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>C</td>
    <td>HTML</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>HTML5</td>
    <td>Yes</td>
    <td>Canada</td>
  </tr>
 <tr>
    <td>M</td>
    <td>Name</td>
    <td>Italy</td>
  </tr>
  <tr>
    <td>North/South</td>
    <td>Var</td>
    <td>UK</td>
  </tr>
</table>

</body>
</html>



PreviousNext

Related