What does bind and unbind mean in jquery?

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

Answers:

Answer

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.

Answer

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

Update:

As of jQuery 1.7, the .on() and .off() methods are preferred to attach and remove event handlers on elements.

Answer

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.

Answer

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.