In addition to this list of standard handlers, you can also leverage bind() to attach jQuery custom handlers— e. g. mouseenter and mouseleave— as well as any custom handlers you may create.
To remove standard handlers or custom handlers, we simply pass the unbind() method the handler name or custom handler name that needs to be removed— e. g. jQuery(' a '). unbind(' click '). If no parameters are passed to unbind(), it will remove all handlers attached to an element.
These concepts just discussed are expressed in the code example below. Sample: sample65. html
<! DOCTYPE html > < html lang =" en "> < body > < input type =" text " value =" click me " /> < br /> < br /> < button > remove events </ button > < div id =" log " name =" log "></ div > < script src =" http:// ajax. googleapis. com / ajax / libs / jquery / 1.7.2 / jquery. min. js "></ script >
< script >( function($) { // Bind events $(' input '). bind(' click ', function() { alert(' You clicked me!'); }); $(' input '). bind(' focus ', function() { // alert and focus events are a recipe for an endless list of dialogs // we will log instead $('# log '). html(' You focused this input!');
}); // Unbind events $(' button '). click( function() { // Using shortcut binding via click() $(' input '). unbind(' click '); $(' input '). unbind(' focus '); // Or, unbind all events // $(' button '). unbind();
});
})( jQuery); </ script > </ body > </ html >
Notes: jQuery provides several shortcuts to the bind() method for use with all standard DOM events, which excludes custom jQuery events like mouseenter and mouseleave. Using these shortcuts simply involves substituting the event ' s name as the method name— e. g.. click(), mouseout(), focus().
70