I'm attempting to use .closest to grab the closest <li>
value based on clicking one of the <li>
elements. I'm not having much luck. First off, I'm not having much luck trying to figure out how to add the .click
part to the .closest
.
Thanks in advance.
<script>
$(document).ready(function() {
var test = $('#test').closest('li').text();
alert(test);
});
</script>
</head>
<body>
<tbody>
<ul id="test">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
tbody
element in thebody
element, that is not correct. Atbody
element is only used as a child to atable
element.closest()
looks for the closest parent, not children of the element. Usechildren()
for immediate children orfind()
for any children of the element.