Posted By

anagaiyahoocom on 04/21/11


Tagged

jquery


Versions (?)

Jquery Callback Without Anonymous Function


 / Published in: JavaScript
 

Is very inefficient to attach a anonymous function event handlers to multiple items in a selection. The function is duplicated for each element.

This shows how you can just create a single function and in the event handler callback just call that function.

  1. $('dt').find('a').click(doit);
  2.  
  3. function doit(e)
  4. {
  5. //e - is event object passed in
  6. e.preventDefault();
  7. //this - is the element this func is attached to
  8. alert($(this).attr('href');
  9. }

Report this snippet  

You need to login to post a comment.