user1889007
user1889007

Reputation: 319

Function inside click event needs 2 clicks to trigger

I have an a link with id #searchBoxPop2. Once its clicked, I want to trigger #searchBoxPop2.toggle function. My problem is I have to click twice on the link for the toggle to trigger. Can you guys please help this dirty script? Thanks a lot

  $('#searchBoxPop2').live('click', function () {
      if (!$('.header-wrapper-home').length) {
          $('div.right-block').show();

              $('#searchBoxPop2').toggle(
              function () {
              $('.detail-page-content').animate({
                  marginTop: "+=290px"
              }, 800)
          },
              function () {
              $('.detail-page-content').animate({
                  marginTop: "0px"
              }, 800);
          });
      }
      if ($('.header-wrapper-home').length) {
          $('.header-wrapper-home').slideToggle('slow');

      }
  });

Please let me know if you want me to explain the rest of the code and what Im going to do in detial.

Upvotes: 0

Views: 80

Answers (1)

Edgar Allan Pwn
Edgar Allan Pwn

Reputation: 311

As I said in the comment, the toggle event function was deprecated in jQuery 1.9. Thus you need to use your own method to alternate calls. At first I was annoyed that a built in alternative didn't exist, but this method is more succinct. Especially since once you have the format down, it's easy to replicate to any other event call.

Here's a quick and dirty callback function I just wrote. There are probably better ways of accomplishing this but it's a good starting point:

function () {
    var number = 2; // the number of events you toggle through
    if (! window.toggle_counter )
        window.toggle_counter = 0;
    switch (window.toggle_counter) {
        case 0:
            // do some things
            break;
        case 1:
            // do other things
            break;
        // etc ..
    }
    window.toggle_counter = (window.toggle_counter + 1) % number;
}

Be aware that window.variable_name is global scope, so be wary of conflicts.

Upvotes: 1

Related Questions