javascript - Accessing the original object inside each -


i want check or uncheck checkboxs following code works me

 $( '#chkboxall' ).live( 'change', function() {                                  objs=$(document).find('#chkbox');                 objs.each(function(){                                 $(this).attr( 'checked', $(this).is( ':checked' ) ? '' : 'checked' );             });                  }); 

but instead of using $(this).is( ':checked' ) ? '' : 'checked' want status of original checkbox have checked ie status of $('#chkboxall' ). how can inside each?

krishnik

you can shorten without .each() here (since .attr() can run on every element in set already):

$('#chkboxall').live('change', function() {   $(document).find('#chkbox').attr('checked', this.checked); }); 

however isn't valid, have repeated chkbox id attribute invalid. give elements class instead, this:

<input name="blah" class="chkbox" type="checkbox" /> 

then use .class selector in code:

$('#chkboxall').live('change', function() {   $('.chkbox').attr('checked', this.checked); }); 

for others finding in different situation, reference "outer this" inside .each(), set variable, this:

$('#chkboxall').live('change', function() {   var thing = this;   $('.chkbox').each(function(){     //thing = outer this, #chkboxall element   }); }); 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -