Tuesday, June 30, 2009

[jQuery] Re: check/uncheck all checkboxes with specific id

definitely defer to experience. Question, have seen you mention attr() is broken,don't use it. I try to learn and absorb as much as possible from here

Is it the performance of attr() or reliablity problem?

Matt Kruse wrote:
On Jun 30, 12:24 pm, "evanbu...@gmail.com" <evanbu...@gmail.com> wrote:   
                $(':checkbox.chkEvent').each(function() {                     var el = $(this);                     el.attr('checked', el.is(':checked') ? '' : 'checked');                   })     
 Avoid attr(), and try to avoid fitting every problem into a jQuery solution...  Try this simple code:  $(':checkbox.chkEvent').each(function() {   this.checked = !this.checked; }  I also keep my "run" plugin handy for simple things like this:  // A General "run" function to simplify coding $.fn.run = function(fn) { 	if (typeof fn=='string') { fn = new Function(fn); } 	this.each(fn); }  Then:  $(':checkbox.chkEvent').run("this.checked = !this.checked");  Whether that's actually more efficient to write depends on the situation ;)  Matt Kruse    

No comments: