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

BILL Carlisle
BILL Carlisle used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
http://api.jquery.com/jQuery.map/

$.map(yourArrayOfIDs, function(id, i) {
    $("#" + id).attr("disabled", true);
})

Open in new window

Commented:
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.
BILL CarlisleAPEX Developer

Author

Commented:
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.

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






Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Top Expert 2004

Commented:
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.
Commented:
Disabled fields are not passed to the web server.
Add the following code somewhere in your script.
$(document).ready(function() {

$('form').submit(function() {
  $(this).find('input[type="checkbox"]').attr('disabled','');
});

});

Open in new window

BILL CarlisleAPEX Developer

Author

Commented:
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??
Top Expert 2004
Commented:
No, jhp333 and I both commented that disabled form fields do not get passed to the back end server. You can try the JavaScript solution given and suggested, and we can help if you have trouble with that. At the same time, read my post above to make sure you understand that your server should do its own check to make sure disabled checkboxes cannot be updated. Otherwise, I can just disable JavaScript and submit any changes I want!
BILL CarlisleAPEX Developer

Author

Commented:
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!
BILL CarlisleAPEX Developer

Author

Commented:
Thx for the help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial