Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript jQuery execute if and else together on document.ready

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #45384
    jiddisch
    Member

    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;
    }
    });
    });

    #138058
    jiddisch
    Member

    none

    #138064
    pixelgrid
    Participant

    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)
  • The forum ‘JavaScript’ is closed to new topics and replies.