2

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.

Mohammad
  • 3,449
  • 6
  • 48
  • 75

4 Answers4

2

Your code works. I have added 2 other solutions to it anyway:

jQuery('.mycart').click(function(e){

    e.preventDefault();
    $(".basket").slideToggle(800);
});

http://codepen.io/damianocel/pen/avjVGQ

I have commented out one, just try and see which one you like best.

Eric
  • 6,563
  • 5
  • 42
  • 66
damiano celent
  • 721
  • 6
  • 12
  • the element display is none and important in the css, so this will no work, the css rule will override it – Mohammad Nov 03 '15 at 08:42
  • It should not, and if, use the fadeToggle method, this sets the display to none by default. So does the slideToggle method btw. – damiano celent Nov 03 '15 at 08:46
1

use this code in your JQuery

$('.mycart').click(function() {
   if($('.basket').is(':visible')) {
       $('.basket').hide()
   }
  else {
       $('.basket').show()
  }
})
Jrey
  • 1,618
  • 2
  • 17
  • 34
1

I found the solution, just by blocking the mouseup() function to hide the container if the clicked target is the anchor $('.mycart'), so the when the user clicks on this anchor, the click() function of this anchor will be applied normally and hide the container if its shown, here is the new jquery:

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 && !jQuery('.mycart').is(e.target)) // ... nor a descendant of the container or .mycart anchor
        {
            container.hide();
        }
    });
Mohammad
  • 3,449
  • 6
  • 48
  • 75
0

Try this...

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');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<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>
Eric
  • 6,563
  • 5
  • 42
  • 66