Thanks Michel - I appreciate the time you took here to provide such a useful answer - javascript algebra in essence.
Cheers, Bruce On Nov 28, 12:18 am, Michel Belleville <michel.bellevi...@gmail.com> wrote: > Now let's try to simplify this a bit : > > $('a.c_update').each(function() { > var $a = $(this); > var q = $a.attr('id'); > $a.bind("click", function() { > doStuff('getitem',q); // hmm, I don't like this variable much... > return false; > > }); > }); > > $('a.c_update').each(function() { > var $a = $(this); > $a.bind("click", function() { > doStuff('getitem', $a.attr('id');); // there, no need > return false; > }); > }); > > $('a.c_update').each(function() { > var $a = $(this); > $a.bind("click", function() { // wait, if I've bound my event to the link, > why bother keep it as a variable before ? > doStuff('getitem', $a.attr('id');); // there, no need > return false; > }); > }); > > $('a.c_update').each(function() { > $(this).bind("click", function() { // feels better > doStuff('getitem', $(this).attr('id');); > return false; > }); > }); > > $('a.c_update').each(function() { // hey, why bother looping, I could do it > all with the .bind() function on the whole collection anyway > $(this).bind("click", function() { > doStuff('getitem', $(this).attr('id');); > return false; > }); > }); > > $('a.c_update').bind("click", function() { // how sleaker > doStuff('getitem', $(this).attr('id');); > return false; > }); > > So, basically you can reduce #1 to #2 breaking nothing, without any > significan loss (in fact I think it's a net gain). > > Michel Belleville > > 2009/11/27 Bruce MacKay <thomasba...@gmail.com> > > > Hello folks, > > > I have some html returned via ajax that contains several 'a' tags to > > which I am binding a function. > > > Both of the following methods does the job, but my perception of other > > posts about this general practice from more wiser folk than me is that > > the first method is the "better" method. Is this the case? Which is > > the "best" method - and more importantly, why? > > > method #1 > > $('a.c_update').each(function() { > > var $a = $(this); > > var q = $a.attr('id'); > > $a.bind("click",function() {doStuff('getitem',q);return > > false;}); > > }); > > > method #2 > > $('a.c_update').bind("click",function() {doStuff > > ('getitem',this.id);return false;}); > > > Thanks, > > Bruce