Mihir
Mihir

Reputation: 8734

toggle function

my question is two functions are passed to toggle why.. and out side another function is there that is also confusing plz tell me

 $('#login a').toggle(function() {
    $(this)
      .addClass('active')
      .next('form')
      .animate({'height':'show'}, {
        duration:'slow',
        easing: 'easeOutBounce'
      });
  }, function() {
    $(this)
      .removeClass('active')
      .next('form')
      .slideUp();
  });
  $('#login form :submit').click(function() {
    $(this)
      .parent()
      .prev('a')
      .click();
  });

Upvotes: 3

Views: 261

Answers (2)

Ivan Prikhach
Ivan Prikhach

Reputation: 21

According to JQuery API documentation functions will be called on alternating clicks:

  • first click - first function,
  • second click - second function,
  • third click - first function,
  • fourth click - second function...

As for handler of submit button, it simulates click for anchor (a) element that is sibling to element that contains the button and is located before container element. Like this:

<a href="#">click will be simulated for this link</a>  
<div>  
    <input type="submit"/>  
</div>

Upvotes: 0

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 263117

The toggle() method takes two (or more) functions as arguments and calls one of them alternately each time the element is clicked.

Functions are first class citizens in Javascript: you can manipulate them like any other object, including passing them to other functions:

function foo(otherfunction)
{
    otherfunction();
}

function bar()
{
    window.alert("bar() was called.");
}

foo(bar);  // Ultimately calls `bar()`.

Upvotes: 1

Related Questions