How can I modify this accordion to close when a user clicks on another link, and also change the image of the open "p"
tag?
Currently, clicking on a link toggles the content and changes the image. However, what I am struggling with is closing the previously opened "p" tag. It currently functions like an onclick toggle for the content.
I attempted to add code that closes all the "p" tags but doesn't revert the image back to the default one with the plus icon.
$('p').hide();
$(this).bind('keypress click', function(e) {
e.preventDefault();
$('p').hide();
var $icon = $(this).find('.icon');
if($icon.hasClass('first-image'))
{
$icon.removeClass('first-image').addClass('second-image');
}
else
{
$icon.removeClass('second-image').addClass('first-image');
}
$(this).next('p').slideToggle();
});
<ul class="accor">
<li><a href="#"><span class="icon second-image"></span> Item 1</a>
<p style="display:block"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</li>
<li><a href="#"><span class="icon first-image"></span> Item 2 </a>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</li>
</ul>
The problem I'm facing is with Item 1 - it closes but the image remains as (--), instead of changing back to (+).