Tuesday, June 30, 2009

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

As previously stated classes would be better. Also of note, @ in attribute selectors is deprecated, but better yet, input:checkbox (which is faster than simply :checkbox I've been told). Also, if you insist on doing ids instead of class, it'd be better to use the "starts with" method than .each and a substr.

On Tue, Jun 30, 2009 at 09:38, evanburen@gmail.com <evanburen@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>

No comments: