jquery
  1. jquery-end

jQuery end()

The end() method in jQuery is used to end the most recent filtering operation or chain and return the set of matched elements to its previous state.

Syntax

The end() method can be used with or without parameters.

Without parameters:

$(selector).filter(criteria1).end();

With parameters:

$(selector).filter(criteria1).next().end(criteria2);

Use

The end() method can be used in chaining to end the most recent filtering operation and return the set of matched elements to its previous state. This can be useful when you want to filter a set of elements, perform an operation on the filtered elements, and then return to the original set of elements.

Example

<div class="container">
  <div class="items">
    <div class="item first">First item</div>
    <div class="item second">Second item</div>
    <div class="item third">Third item</div>
  </div>
</div>

<script>
$(document).ready(function(){
  $(".items").find(".second").css("background-color", "red").end().css("background-color", "blue");
});
</script>
Try Playground

In this example, we select the element with the class of .items and use the find() method to get the element with the class of .second. We then apply a CSS style of background-color: red to the .second element. Finally, we use the end() method to return the set of matched elements to its previous state (.items) and apply a CSS style of background-color: blue to the original set of elements.

Summary

The end() method is a useful function in jQuery that allows you to end the most recent filtering operation and return the set of matched elements to its previous state. This can be helpful in chaining operations and modifying the DOM elements of a webpage. Try it out for your next jQuery project!

Published on: