I'm trying to implement an accordion. I'm having a problem with comparing an element in the .each() loop.
I start by finding the .accordion
who's .content
I need to expand. This is the parent of the .accordion .title
element that was click (line 6 below).
I then loop through each .accordion
on the page. I try to detect which of those .accordion
is the parent
, to expand his .content
. This isn't working.
// ACCORDION
var accordions = $('.accordion').toArray();
// Scroll down on click
$('.accordion .title').click(function() {
var parent = $(this).parent();
$(accordions).each(function() {
// Show this accordion if it's the parent of the .title clicked
// Problem is here ********
if ( $(this) == parent )
$(this).children('.content').slideDown();
// Hide all accordions except this parent
else {
$(this).children('.content').slideUp();
}
});
});
My main question is a bit wider than this particular example:
How can I figure out if $(this) == some_var_pointing_to_an_element
when looping through an array of elements like above?
EDIT: My accordion html if it's relevant:
<div class=".accordion">
<div class=".title">Hello</div>
<div class=".content">World</div>
</div>