Dope Dope - 4 months ago 15x
jQuery Question

Why isn't my Jquery closest function working

So I have this function:

var test = $('#currentCat').closest('div');

And here is the HTML

<div class="collapse" id="26">
<a href="#" class="list-group-item" id="currentCat">Hoodies (6)</a>

But instead of giving me HTMLDivElement I get Object and it's not working

var test = $('#currentCat').closest('div');

jQuery does not return DOM elements. It returns a jQuery object, which is also an array, which CONTAINS DOM elements.

Do this:

var $list = $('#currentCat').closest('div');
var element=$list[0]; // Get first item from the array

Pro debugging hint:

Next time you have a question like this, examine the contents you're looking at.
Had you done a console.log(test), you could have seen it was an array (or array-like) structure.