Page Content

Tutorials

How to Use :Lt Index Selector in jQuery With Code Example

:Lt Index Selector in jQuery

The :lt(index) selector in jQuery selects elements from matched elements with index values less than a specific index. This selection is very useful because it can filter objects by position.

Syntax:

Syntax for the :lt index selector is often:

$(‘selector:lt(index)’),
  • Any valid CSS selector, such as #myid,.myclass, or p, li, is a “selector”.
  • The index is a zero-based integer that indicates the element selection limit. This means the elements at the index will only be in the selection preceding it.

How It Works

Using :lt index selector , jQuery acts on the first elements the selection expression matches.

Zero-Based Indexing: As in other programming situations, jQuery counts items from 0 rather than 1. This is key for zero-based indexing. The first element has index 0, the second 1, etc.

“Less Than”:  The :lt means “less than”. The function $(‘li:lt(2)’) selects items with indexes 0 and 1 (first and second), but excludes the third element at index 2. This is like saying it picks every element before the third. In a same manner, $(‘p:lt(3)’) would select the first three <p> items (those with indices 0, 1, and 2), thereby choosing elements first.

Filtering: The initial collection of matched items is reduced to just those that satisfy the positional condition by this selector, which also serves as a filtering selection.

Code Example

The operation of :lt(index) will be illustrated by looking at a tiny application. We’ll use an unordered list style for the first two entries.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>jQuery :lt(index) Example</title>
  <!-- Include jQuery from CDN -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 20px;
    }
    ul {
      list-style-type: none;
      padding: 0;
    }
    li {
      padding: 8px;
      margin-bottom: 5px;
      background-color: #f0f0f0;
          }
    .highlight {
      background-color: #ffe0b2;
           color: #d35400;
      font-weight: bold;
    }
  </style>
  <script>
    $(document).ready(function() {
      // Highlight list items with index less than 2 (i.e., index 0 and 1)
      $("li:lt(2)").addClass("highlight");
    });
  </script>
</head>
<body>
  <h1>List Items Example</h1>
  <ul>
    <li>List Item 1 (Index 0)</li>
    <li>List Item 2 (Index 1)</li>
    <li>List Item 3 (Index 2)</li>
    <li>List Item 4 (Index 3)</li>
    <li>List Item 5 (Index 4)</li>
  </ul>
</body>
</html>

Output:

List Items Exampl
List Item 1 (Index 0)
List Item 2 (Index 1)
List Item 3 (Index 2)
List Item 4 (Index 3)
List Item 5 (Index 4)

Explanation of the Code

HTML Structure: An unordered list (<ul>) with five items (<li>) and a heading (<h1>) make up the HTML structure.

jQuery Library: CDN jQuery library is located in the <script> tag in the <head> section. Using jQuery functions is possible.

$(document).ready(function() { … });:This jQuery construct is typical. This method runs its logic only when the HTML content loads and the DOM is ready for interaction.

$(“li:lt(2)”):

  • The “li” component selects all <li> elements in HTML content.
  • The :lt filter is denoted by “:lt(2)”. As its index parameter, it accepts 2. The rule chooses all <li> elements with an index smaller than 2. Because indexing begins at zero, this comprises:
  • The <li> element at index 0 in this instance is “List Item 1”.
  • “List Item 2” (Index 1) contains the <li> element.
  • Elements after that will not be chosen, including the element at index 2 (“List Item 3”).

.addClass(“highlight”): The chosen items cause this jQuery function to be called. Every element in the current jQuery selection gains the CSS class “highlight” as a result of this. The <style> block’s highlight CSS class gives selected list items bold orange text, an orange border, and a light orange backdrop.

To view the whole filtering capabilities of the:lt index selector, open this HTML page in a browser. Using their zero-based indices (0 and 1), the jQuery code will recognise “List Item 1” and “List Item 2” when the page is ready and apply “highlight” decoration.

Conclusion

In conclusion, the jQuery :lt index selector offers a strong and fast approach to filter and alter elements by position. By picking components with an index less than a certain number, developers can quickly target certain areas of matched elements, such as the first few list items in a collection. This selector excludes the element at the provided index using zero-based indexing. In the example, $(“li:lt(2)”) highlights the first two elements (indices 0 and 1), demonstrating the use of :lt index selector for conditional styling or HTML manipulation. It is especially useful in dynamic web applications that control element display or behaviour based on DOM order.

Kowsalya
Kowsalya
Hi, I'm Kowsalya a B.Com graduate and currently working as an Author at Govindhtech Solutions. I'm deeply passionate about publishing the latest tech news and tutorials that bringing insightful updates to readers. I enjoy creating step-by-step guides and making complex topics easier to understand for everyone.
Index