Maloke Maloke - 1 year ago 78
Javascript Question

closest() not selecting the element


<h2 class="tm section-title margin-l-10">Normal Cards<button class="tm bg-dark btn-tiny margin-b-10 float-r js-show-code">show code</button></h2>
<div class="grid-100">
&lt;div class=&quot;<span class="tm bg-win blue">tm card</span>&quot;&gt;
<span class="tm lightyellow">...</span>



I'm still learning Javascript and Jquery, and I have no clue what i'm doing wrong. I can't select the

It always returns:
[prevObject: r.fn.init[1]]

Answer Source

closest() get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree. You are looking for an element which is not related with button.js-show-code.



Here is the fiddle

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download