Grow your CSS skills. Land your dream job.

jQuery execute if and else together on document.ready

  • # June 8, 2013 at 5:12 am

    I want to remember with jquery the user’s choice using cookie.

    I Installed the jquery.cookie plugin and it works grate, but the problem is that it’s working only if I put the script on the bottom of the html page, but if I put the code in an external file into document.ready, when I click on the element I got like two clicks, the if and the else is working together. What can be the solution?

    # This is the html:

    Hi

    How are you?

    # And this is the jquery:
    $(function() {
    $(‘.hi’).click(function() {
    if ($.cookie(‘viewState’) === ‘off’ || !$.cookie(‘viewState’)) {
    $(‘.how’).css(‘display’, ‘block’);
    $.cookie(‘viewState’, ‘on’);
    //return false;
    } else {
    $(‘.how’).css(‘display’, ‘none’);
    $.cookie(‘viewState’, ‘off’);
    //return false;
    }
    });
    });

    # June 8, 2013 at 5:13 am

    none

    # June 8, 2013 at 9:12 am

    it might be because click event can fire multiple times you can try listening for mouseup instead of click

    $(‘.hi’).bind(‘mouseup’,function(){});

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".