views:

128

answers:

2

I had a requirement where I need to clear off the "select all" checkbox in case user manually deselects any of the data rows. This has been accomplished by detecting this during an onRowSelect (jqgrid event) event. The code snippet is as below and works as expected.

onSelectRow: function(){$("input:checkbox[id='cb_jqg']").removeAttr('checked');}

The thing I wonder about is whether I should check the checkbox for already selected before I clear it off or can I simply clear it (as it does not have any impact) as done above.

Is there any performance / code ethic issues with the syntax I used?

+3  A: 

Adding a check before setting the value will be slower than just arbitrarily setting them all simply because it has to do the check.

Ethically, it's not gonna throw an error, so all's fair in love and coding, right?

Gabriel Hurley
Thanks for reply. This aligns with my reasoning while I was doing writing the code.Later I realise that the "select all" state is not that frequent, so will it be better just to have a check to see if its checked (and since more often than not it is not checked) do nothing.Any thoughts on this ?
Nrj
A: 

This looks like you have multiple checkboxes with the same id. This is invalid in HTML. You could instead use the same name for these checkboxes.

Also, the more standard way of setting the checkedness of a checkbox is simply setting the checked property of the checkbox element to true or false, rather than rely on jQuery's attribute handling methods.

Tim Down