jQuery Find/Get Closest Element

jQuery find closest element; In this tutorial, you will learn how to get or find the first ancestor or closets elements of selected HTML elements (div, span, button, paragraph, textarea, tag etc) by class, id, tag.

jQuery closest() Method

The closest () method of jQuery gives the first ancestor of the selected HTML element.

The closest method of JQuery accepts a mandatory parameter, which is used to filters the traversal. This method is starts traversal by find at the current element, it will return the current html element if it matches the specified expression, it will expand the DOM tree unless it detects an element that matches the filter.

Syntax closest() Method

$("selector").closest(first)

This returns the first ancestor of the selected HTML element.

$(selector).closest(first,second)

It returns the first ancestor using the DOM reference to use the DOM tree.

Parameters

  • First :- It is required. A selector specifies expression, element, or jquery object to reduce the search of ancestors.
  • Second :- It is optional. A DOM element within which a matching element may be found.

Example of jquery find/get closest element by tag

This example will demostrate you how use closest method to selected html elements.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery closest method</title>
  <style>
  .tuts {
    margin: 10px;
    background: grey;
  }
  li.lightcss {
    background: green;
  }
  </style>
 <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>

<ul>
  <li class="tuts"><b>Click me!</b></li>
  <li class="tuts">You can also <b>Click me!</b></li>
</ul>

<script>
$( document ).on( "click", function( event ) {
  $( event.target ).closest( "li" ).toggleClass( "lightcss" );
});
</script>

</body>
</html>

Demo of jquery find/get closest element by tag

jQuery closest method
  • Click me!
  • You can also Click me!

In the above example of the closest () method, you will click on the elements and the background color of this element will change. We also use the jquery toggleClass () method along this method.

Recommended jQuery Tutorials

  1. To Find Siblings Elements Using jQuery sibling methods
  2. jQuery | Event MouseUp By Example
  3. Event jQuery Mouseleave By Example
  4. jQuery Event Mouseenter Example
  5. Event jQuery MouseOver & MouseOut By Example
  6. keyup jquery event example
  7. Jquery Click Event Method with E.g.
  8. Event jQuery. Blur By Example
  9. jQuery form submit event with example
  10. keydown function jQuery
  11. List of jQuery Events Handling Methods with examples
  12. Jquery Selector by .class | name | #id | Elements
  13. How to Get the Current Page URL in jQuery
  14. jQuery Ajax Get() Method Example
  15. get radio button checked value jquery by id, name, class
  16. jQuery Set & Get innerWidth & innerHeight Of Html Elements
  17. jQuery Get Data Text, Id, Attribute Value By Example
  18. Set data attribute value jquery
  19. select multiple class in jquery
  20. How to Remove Attribute Of Html Elements In jQuery
  21. How to Checked Unchecked Checkbox Using jQuery
  22. jQuery removeClass & addClass On Button Click By E.g.
  23. To Remove whitespace From String using jQuery
  24. jQuery Ajax Post Method Example
  25. jQuery Ajax Get Method Example
  26. To Load/Render html Page in div Using jQuery Ajax $.load

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *