jQuery - Click handler and remove elements to assign

Click handler functions are probably one of the most used when working with jQuery. Rarely get there without them.
I want you today to present briefly a few times finer points to do so.

Click an item to add a handler
An element a click handler mitzugeben is probably the easiest.

? View Code JAVASCRIPT
  1
 2
 3
  ) . click ( function ( ) { $ ('# IdDesElements'). Click (function () (
 "Click handler ausgelöst!" ) ; alert window. ("Click triggered handler");
 )); 

Mehrer Click Add a handler element
An element can account more than just a Click handler assigned.
The item is clicked, any handler in turn exported to.

? View Code JAVASCRIPT
  1
 2
 3
 4
 5
 6
  ) . click ( function ( ) { $ ('# IdDesElements'). Click (function () (
 "Click handler 1!" ) ; alert window. ("Click a handler!");
 ));
 ) . click ( function ( ) { $ ('# IdDesElements'). Click (function () (
 "Click handler 2" ) ; alert window. ("Click handler two");
 )); 

Click remove handler
Click again to remove handlers a simple enough unbind.

? View Code JAVASCRIPT
  1
  ) . unbind ( 'click' ) ; $ ('# IdDesElements'). Unbind ('click'); 

Click Add handler and also remove
Since we have the wonderful with jQuery so the chaining method for disposal, we can avoid an element of a previously assigned to him all the Click handler and simultaneously add a new one.

? View Code JAVASCRIPT
  1
 2
 3
 ) . unbind ( 'click' ) . click ( function ( ) { window. alert ( "neuer Click handler ausgelöst!" ) ; } ) ; $ ('# IdDesElements'). Unbind ('click'). Click (function () (window. Alert ("Click handler triggered new!");)); 

Click handler and anchor
When you click a link to an anchor handler calls but do not want that the browser follows the link, after the handler is called false must also simply a return;

? View Code JAVASCRIPT
  1
 2
 3
 4
  ) . click ( function ( ) { $ ('# IdDesElements'). Click (function () (
 "Click handler" ) ; alert window. ("Click handler");
     ; return false;
 )); 
http://www.mysrc.de/wp-content/plugins/sociofluid/images/twitter_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/google_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/delicious_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/digg_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/reddit_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/dzone_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/blinklist_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/blogmarks_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/furl_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/newsvine_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/technorati_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/magnolia_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/myspace_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/facebook_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://www.mysrc.de/wp-content/plugins/sociofluid/images/sphinn_48.png

Related Articles

Comments