Link to home
Create AccountLog in
Avatar of BILL Carlisle
BILL CarlisleFlag for United States of America

asked on

Checkboxes - disable all that are in an array or comma delimited list

Hi All,
I want to disable checkboxes that have an id from an array or comma delimited list using JQuery.
Thanks, Bill
<input id="P2_IERS_0" name="p_v16" value="2901" type="checkbox"><label for="P2_IERS_0">ETP</label></td><td>
<input id="P2_IERS_1" name="p_v16" value="3007" type="checkbox"><label for="P2_IERS_1">AMAI</label></td><td>
<input id="P2_IERS_2" name="p_v16" value="2902" type="checkbox"><label for="P2_IERS_2">AIN</label></td><td>
<input id="P2_IERS_3" name="p_v16" value="2903" type="checkbox"><label for="P2_IERS_3">AT</label></td></tr>
<tr>
<td>
<input id="P2_IERS_4" name="p_v16" value="2904" type="checkbox"><label for="P2_IERS_4">ACW</label></td><td>
<input id="P2_IERS_5" name="p_v16" value="326" type="checkbox"><label for="P2_IERS_5">Auson</label></td><td>

<input id="P2_IERS_6" name="p_v16" value="2905" type="checkbox"><label for="P2_IERS_6">ASW</label></td><td>
<input id="P2_IERS_7" name="p_v16" value="327" type="checkbox"><label for="P2_IERS_7">COAT</label></td></tr>
<tr>
<td>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
The following will do the same:
$("#"+yourArrayOfIDs.join(",#")).attr("disabled", "disabled");

Or if you can get the list_string like "#P2_IERS_1, #P2_IERS_3, #P2_IERS_5",
simply
$(list_string).attr("disabled", "disabled");
will do the job.
Avatar of BILL Carlisle

ASKER

Hi, awesome.. I used the map with function and added a bit more..

$.map(yourArrayOfIDs, function(id, i) {
    jQuery('input[id^="P2_IERS_"][value="'+id+'"]').attr("disabled", true);
})

Now, loading the checkbox, disabling 40 of them, some checked some not, then I as the user changes the ones that are not disabled and saves the form. I want all the checks - enabled or disabled to be saved.

But all the checks of those checkboxes that were disabled were saved as unchecked.

?????????????






Please post the part of your server side code which saves the checks for your checkboxes. My guess is that something is happening there. Values of disabled form fields are not passed to the backend on form submit, so you may need to take that into account.

An alternative option is to intercept form submit through the onsubmit handler, and then temporarily enable all the disabled checkboxes before submitting for real through JavaScript.

By the way, remember that if it is important that the user be unable to change the value of disabled checkboxes, you need to do separate server-side validation that this is the case, because it is easy for anyone to disable JavaScript.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
I put debug before it hits my back end process.

no disable
SUPPL[2901:3007:2902:326:327:328:325:331:332:147:2908:55:3009:2909:333:2910:2912:2913:2916:104:2917:2919:3012:338:339:2920:2921:340:2924:2925:341:2927]

with disable
SUPPL[2901:3007:2902:326:327:328:325:331:332:147:2908:55:3009:2909:333:2910]

Its not getting the full list.
I use Oracle Apex so may be on their side??
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Yes, I see what you mean.. I have a validation to check in case someone hacked the disabled status but the real solution is to have the backend process absolutely not touch what the user has no access...

Got it! Thanks!
Thx for the help!