views:

638

answers:

4

What does bind and unbind mean in jquery in idiot slow learner terms?

+5  A: 

In simple terms: for binding and unbinding event handlers to elements.

$("#divElement").bind('click', functionName);

binds a click event handler to the element with id divElement

$("#divElement").unbind('click', functionName);

unbinds a click event handler to the element with id divElement

Edit:

Bind also allows you to bind a handler to one or more events.

$("#divElement").bind("click dblclick mouseout", function(){ // your code });
rahul
Might want to include the fact that you can bind a handler to multiple events in one bind command
Russ Cam
Edited my post.
rahul
Also might want to add that you can bind to arbitrary event names like `$("div").bind("updatePage", function() {...});` then trigger those events with $("div").trigger("updatePage");
John Sheehan
This article - http://brandonaaron.net/blog/2009/03/26/special-events is a good introduction to special events
Russ Cam
+2  A: 

Binding: coupling an handler to an element(s), that will run when an event occurs on said element(s). Depending on what kind of event you want to handle you'd use different functions like click(function) (alt: bind('click', function) or focus(function) (alt: bind('focus', function).

Unbinding: de-coupling of an handler from an element(s), so that when an event occurs the handler function will no longer run. Unbinding is always the same; unbind('click', function) to unbind a certain handler, unbind('click') to unbind ALL click handlers, and unbind() to unbind ALL handlers. You can substitute click for other types of events of course.

Darko Z
A: 

Bind attaches a piece of code to be run to a given HTML element (which is run on the supplied event). unbind removes it.

krosenvold
+2  A: 

In three sentences:

An event is a signal that is visible in your program - a key press, for example.

A handler is a function that is geared towards reacting to a certain event.

Binding associates a handler with an event, unbinding does the opposite.

Tomalak