Tuesday, June 30, 2009

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

going back to the OP there is an easy method to get "starts with", and it is not necessary use each() or if  to check them

$("input[id^='chkEvent']").attr("checked","checked");
this will check *all * starting with ID = chkEvent


evanburen@gmail.com wrote:
Thanks.  I still don't have something quite right because this isn't working. I've added the class property to each of my event checkboxes.  <script type="text/javascript"> 	//check all checkbox just for the events     $(document).ready(function() {             $('#toggleEvents').click(              function() {                 $(':checkbox.chkEvent').each(function() {                     var el = $(this);                     el.attr('checked', el.is(':checked') ? '' : 'checked');                   })            });          }); </script>  On Jun 30, 12:58 pm, Eric Garside <gars...@gmail.com> wrote:   
Is there a particular reason you couldn't use classes to do this? Instead of the markup you provided, something like:  <input type="checkbox" value="EventAcceleratedOptionVesting" id="chkEventAcceleratedOptionVesting" class="chkEvent" /> <input type="checkbox" value="AccountingChanges" id="chkEventAccountingChanges" class="chkEvent" />  Then:  // Use the :checkbox instead of the old method you're using $(':checkbox.chkEvent').each(funciton(){     var el = $(this);     el.attr('checked', el.is(':checked') ? '' : 'checked');  })  Would do what you're asking.  If you insist on doing it with the IDs, you can always just substr:  if ($(this).attr('id').substr(0, 8) == 'chkEvent') ...  But this method is far less efficient, and you'll be double-looping through the tree section (once for jQuery to build an indiscriminate collection of the nodes, then once again by your code to discriminate the nodes and apply the check/uncheck effect)  On Jun 30, 12:38 pm, "evanbu...@gmail.com" <evanbu...@gmail.com> wrote:        
I want to check all of my checkboxes in my form that have an id that begins with 'chkEvent'.  Thanks       
<script language="Javascript" type="text/javascript"> $(document).ready(function() {    $("#toggleEvents").click(function(event){       $('input[@type=checkbox]').each( function() {       
        // Begin this is where I am lost         if($(this).id()=='chkEvent*')         // End this where I am lost         this.checked = !this.checked;       });    });});       
</script>       
<a href="#" id="toggleEvents">Toggle Events</a>       
<form method="post" action=""> <input type="checkbox" value="EventAcceleratedOptionVesting" id="chkEventAcceleratedOptionVesting" /> <input type="checkbox" value="AccountingChanges" id="chkEventAccountingChanges" /> <input type="checkbox" value="AnnualMeetingChanged" id="chkEventAnnualMeetingChanged" /> <input type="checkbox" value="AssetSalePurchase" id="chkEventAssetSalePurchase" /> <input type="checkbox" value="AuditorChange" id="chkEventAuditorChange" /> </form>- Hide quoted text -       
- Show quoted text -     
   

No comments: