1

I have a question regarding the working of jQuery bind. Whenever we use the bind method on any element and write some callback code, does that complete code get executed on the bind event "each time" OR does jQuery only remember the output/result of that code and executes only that result..

Also can we generate another event inside the callback code?

Like how does it work exactly.

1
  • Its unclear to me what your asking for related to bind Commented Aug 23, 2011 at 18:33

3 Answers 3

1

Every time an event is triggered, all functions bound to the event on the specified target get executed. Return values for those callback functions are used to determine if the event should continue to propagate.

There are a few significant differences between a function invoked as part of a script and an event callback:

  1. jQuery normalizes the callback so that it executes in the context of the bound target (i.e. this refers to the bound element that the event was assigned to)
  2. callback parameters are automatically provided
  3. events execute asynchronously when the event is fired, so there may be some unintended consequences with variable values (loop indices like i may have their final value by the time the callback is called)

As callbacks are just functions, they can do anything that any other function can do, including calling trigger. Do be careful not to cause yourself an infinite loop with recursive function calls to the same event.

You wouldn't want to call:

function foo()
{
  foo();
}

Likewise, you wouldn't want to call:

$('#foo').click(function(){
  //short form for .trigger('click')
  $(this).click();
});
Sign up to request clarification or add additional context in comments.

Comments

1

the callback function is evaluated from scratch on every event here:

http://jsfiddle.net/turaaa/wxBF4/

you can see that each click generates a different result.

and yes you can easily generate another event inside the callback

1 Comment

see my answer... .bind() doesn't work on an element added to the DOM at a later time. So if the callback adds an element to the DOM it must also perform the bind() or .live()/.delegate() would have to be used ahead of time.
-1

You can easily generate another event with .trigger().

Edit: After reading and rereading your first question. I believe the answer you're looking for is that bind() is executed each time. However, in a situation where the callback code changes the DOM it is possible for an element to be added where an event on that element wont't be bound.

Consider code that adds a button <input id="mybutton" type="button".... If you use $("#mybutton").bind("click") in all your code this new element will not have any events bound. In order to get a "click" to do something, you'd either have to bind it AFTER it's been added to the DOM or you'd have to use .live() or .delegate() instead of .bind().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.