jQuery - set checkbox
Match checkbox control by name and value. Where name index digit is removed for match.
by Ben Clayton
HTML
This code may only work for cb_case_details.case_status as the "value" property is not given the database id consistently, in most cases just value=1.
<br/><br/>
new <input type="checkbox" name="cb_case_details.case_status-0" value="250" />
open <input type="checkbox" name="cb_case_details.case_status-1" value="251" />
closed <input type="checkbox" name="cb_case_details.case_status-2" value="252" />
pending <input type="checkbox" name="cb_case_details.case_status-3" value="253" />
duff <input type="checkbox" name="cb_case_details.case_status-4" value="254" />
JavaScript
$(document).ready(function() {
var setname = "cb_case_details.case_status-2"; // -2 not used in control name match
var setvalue = 252;
/* --------------------------------------------------*/
var shortname = setname.replace(/-\d+$/, '');
var $control = $("[name*='" + shortname + "']");
if (!$control.length) {
console.error("no control matched");
}
if ($control.first().attr('type') == 'checkbox') {
var $chkbox = $control.siblings("[value='" + setvalue + "']");
if (!$chkbox.length) {
console.error("no specific control matched");
}
$chkbox.attr('checked',true);
} else {
console.error ("other types of control");
}
})