I have a problem with .closets() in jQuery
On jQuery documentation tell that I can use like it.
<!DOCTYPE html>
<html>
<head>
<style></style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<ul><li></li><li></li></ul>
<script>
var close = $("li:first").closest(["ul","body"]);
alert(close.length);
</script>
</body>
</html>
http://api.jquery.com/closest/#closest2
In this part I get information about ul and body, two steps up of li
var close = $("li:first").closest(["ul","body"]);
But it doesn’t work, inside the close don’t have anything, it’s empty, what I’m doing wrong?
The code from jQuery documentation is wrong too. The example doesn’t work.
If you are trying to select the closest ul and the closest body, you should instead use
.parents()and:firstsince the method you are currently trying to use has been removed in recent versions of jQuery.http://jsfiddle.net/MYjRm/
.closest()by definition can only select 1 element, since you’re looking for two, you should use.parents()which can select more than 1.If you simply want to select 1 element, use
.closest()with a selector that will select that one element.$("li:first").closest("ul");