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

JavaScript

Avatar of undefined
Last Comment
BILL Carlisle
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of jhp333
jhp333
Flag of United States of America image

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
BILL Carlisle
Flag of United States of America image

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.

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






Avatar of Zyloch
Zyloch
Flag of United States of America image

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
Avatar of jhp333
jhp333
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of BILL Carlisle
BILL Carlisle
Flag of United States of America image

ASKER

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
Avatar of Zyloch
Zyloch
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of BILL Carlisle
BILL Carlisle
Flag of United States of America image

ASKER

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!
Avatar of BILL Carlisle
BILL Carlisle
Flag of United States of America image

ASKER

Thx for the help!
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo