[jQuery] Re: check/uncheck all checkboxes with specific id
Hi, Charlie solution is very elegant $(input[id^='chkEvent']).attr(checked,checked); But I agree that you must use a class in your markup, so it it's the easier way regards - Original Message - From: Charlie To: jquery-en@googlegroups.com Sent: Tuesday, June 30, 2009 1:34 PM Subject: [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
[jQuery] Re: check/uncheck all checkboxes with specific id
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){ $('inp...@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=toggleEventsToggle 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
[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 withhttp://docs.jquery.com/Selectors/attributeStartsWith#attributevalue method than .each and a substr. On Tue, Jun 30, 2009 at 09:38, evanbu...@gmail.com evanbu...@gmail.comwrote: 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){ $('inp...@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=toggleEventsToggle 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
[jQuery] Re: check/uncheck all checkboxes with specific id
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){ $('inp...@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=toggleEventsToggle 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 -
[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 evanbu...@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:58pm, 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:38pm, "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){ $('inp...@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 -
[jQuery] Re: check/uncheck all checkboxes with specific id
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
[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:24pm, "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