I want to show a certain div onclick of anchor, then I want to hide the div onclick of anchor again, here is my code:
jQuery('.mycart').click(function(e){
e.preventDefault();
var target = jQuery(".basket");
if(target.is(':visible'))
jQuery('.basket').css('cssText', 'display: none !important');
else
jQuery('.basket').css('cssText', 'display: block !important');
});
jQuery(document).mouseup(function (e){
var container =jQuery(".basket");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.hide();
}
});
html:
<a class="cart mycart" href="#">My Cart</a>
<div class="basket">
<span class="callout-arrow"></span>
<div class="basketcount"><span>5</span></div>
<button type="button" class="checkoutbtn">checkout</button>
</div>
the div is shown successfully onclick, but its not hide again. the problem is caused by the second function that is hiding the container when the user clicks outside it.