.closest() | jQuery API Documentation (2024)

Categories: Traversing > Tree Traversal

Contents:

  • .closest( selector )
    • .closest( selector )
    • .closest( selector [, context ] )
    • .closest( selection )
    • .closest( element )
  • .closest( selectors [, context ] )
    • .closest( selectors [, context ] )

.closest( selector )Returns: jQuery

Description: For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.

  • version added: 1.3.closest( selector )

    • selector

      Type: Selector

      A string containing a selector expression to match elements against.

  • version added: 1.4.closest( selector [, context ] )

    • selector

      Type: Selector

      A string containing a selector expression to match elements against.

    • context

      Type: Element

      A DOM element within which a matching element may be found.

  • version added: 1.6.closest( selection )

    • selection

      Type: jQuery

      A jQuery object to match elements against.

  • version added: 1.6.closest( element )

    • element

      Type: Element

      An element to match elements against.

Given a jQuery object that represents a set of DOM elements, the .closest() method searches through these elements and their ancestors in the DOM tree and constructs a new jQuery object from the matching elements. The .parents() and .closest() methods are similar in that they both traverse up the DOM tree. The differences between the two, though subtle, are significant:

.closest() .parents()
Begins with the current element Begins with the parent element
Travels up the DOM tree until it finds a match for the supplied selector Travels up the DOM tree to the document's root element, adding each ancestor element to a temporary collection; it then filters that collection based on a selector if one is supplied
The returned jQuery object contains zero or one element for each element in the original set, in document order The returned jQuery object contains zero or more elements for each element in the original set, in reverse document order

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<ul id="one" class="level-1">

<li class="item-i">I</li>

<li id="ii" class="item-ii">II

<ul class="level-2">

<li class="item-a">A</li>

<li class="item-b">B

<ul class="level-3">

<li class="item-1">1</li>

<li class="item-2">2</li>

<li class="item-3">3</li>

</ul>

</li>

<li class="item-c">C</li>

</ul>

</li>

<li class="item-iii">III</li>

</ul>

Suppose we perform a search for <ul> elements starting at item A:

1

2

3

$( "li.item-a" )

.closest( "ul" )

.css( "background-color", "red" );

This will change the color of the level-2 <ul>, since it is the first encountered when traveling up the DOM tree.

Suppose we search for an <li> element instead:

1

2

3

$( "li.item-a" )

.closest( "li" )

.css( "background-color", "red" );

This will change the color of list item A. The .closest() method begins its search with the element itself before progressing up the DOM tree, and stops when item A matches the selector.

We can pass in a DOM element as the context within which to search for the closest element.

1

2

3

4

5

6

7

var listItemII = document.getElementById( "ii" );

$( "li.item-a" )

.closest( "ul", listItemII )

.css( "background-color", "red" );

$( "li.item-a" )

.closest( "#one", listItemII )

.css( "background-color", "green" );

This will change the color of the level-2 <ul>, because it is both the first <ul> ancestor of list item A and a descendant of list item II. It will not change the color of the level-1 <ul>, however, because it is not a descendant of list item II.

Examples:

Show how event delegation can be done with closest. The closest list element toggles a yellow background when it or its descendent is clicked.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>closest demo</title>

<style>

li {

margin: 3px;

padding: 3px;

background: #EEEEEE;

}

li.highlight {

background: yellow;

}

</style>

<script src="https://code.jquery.com/jquery-3.7.0.js"></script>

</head>

<body>

<ul>

<li><b>Click me!</b></li>

<li>You can also <b>Click me!</b></li>

</ul>

<script>

$( document ).on( "click", function( event ) {

$( event.target ).closest( "li" ).toggleClass( "highlight" );

});

</script>

</body>

</html>

Demo:

Pass a jQuery object to closest. The closest list element toggles a yellow background when it or its descendent is clicked.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>closest demo</title>

<style>

li {

margin: 3px;

padding: 3px;

background: #EEEEEE;

}

li.highlight {

background: yellow;

}

</style>

<script src="https://code.jquery.com/jquery-3.7.0.js"></script>

</head>

<body>

<ul>

<li><b>Click me!</b></li>

<li>You can also <b>Click me!</b></li>

</ul>

<script>

var listElements = $( "li" ).css( "color", "blue" );

$( document ).on( "click", function( event ) {

$( event.target ).closest( listElements ).toggleClass( "highlight" );

});

</script>

</body>

</html>

Demo:

.closest( selectors [, context ] )Returns: Arrayversion deprecated: 1.7, removed: 1.8

Description: Get an array of all the elements and selectors matched against the current element up through the DOM tree.

  • version added: 1.4.closest( selectors [, context ] )

    • selectors

      Type: Array

      An array or string containing a selector expression to match elements against (can also be a jQuery object).

    • context

      Type: Element

      A DOM element within which a matching element may be found.

This signature (only!) is deprecated as of jQuery 1.7 and removed in jQuery 1.8. It was primarily meant to be used internally or by plugin authors.

.closest() | jQuery API Documentation (2024)

References

Top Articles
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated:

Views: 5257

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.