I have been searching everywhere for a solution and I am hoping someone in this community can assist me. I am working with a script that triggers when a checkbox is selected from a group of checkboxes. Each checkbox is given a value of "customer id".
<input type="checkbox" name="inv_add[]" onclick="setcid(\''.$result['cid'].'\')" value='.$result['id'].'/>
The purpose of the JavaScript function is to set a value in a hidden field, which will then inform the next page about the customer id of all checked boxes.
If a user selects a checkbox that belongs to a different customer (i.e., it does not belong to the group), I want to alert the user and then unselect the last box they checked. I'm able to display the alert message, but I am struggling with unchecking the box that was just selected by the user.
function setcid(cid) {
if (window.set_x === undefined) {
sethidden = document.getElementById('cid');
sethidden.value = cid;
set_x = cid;
alert ("Finished setting x");
}
else if (cid !== set_x){
alert ("You are attempting to add two different companies to the same invoice");
/* Uncheck the recently selected box by the user */
}
else {
alert("They are the same");
/* No modification needed */
}
}