Hilary Hilary - 3 months ago 7
HTML Question

How to make a button function as both open and close?

I want to apply a close state to the same hamburger button with an open state already applied. Close state is already applied to another button at a different location.. but I don't want to change that. I just want to duplicate the close state on the open button



</script>
<script type='text/javascript'>
$(function() {
$('.menu').click(function () {
$('#css-menu').css({right:'0'});
});
$('.close-menu').click(function() {
$('#css-menu').css({right:'-340px'});
});
});
</script>


<li class='menu' title='Menu'></li>


Would appreciate if any help

Thanks in advance...

Answer

You are being slightly unclear but I think you mean you want to use JQuery's toggleClass, or, I would use a click record, and check whether the count is even or odd: open or closed.

$(function() {  
    var clickCount = 0;
    $('.menu').click(function () {
        if(clickCount%2==0){
              //do when open
               $('#css-menu').css({right:'0'});
        }else{
             //do when closed
             $('#css-menu').css({right:'-340px'});
         }
        clickCount++;
    });
});

Hope this helps.

Comments