1

I have looked all over for an answer and hope someone here can help. I have a script that runs when you check a check box from a group of checkboxes. The checkboxes are assigned a value of "customer id"

<input type="checkbox" name="inv_add[]" onclick="setcid(\''.$result['cid'].'\')" value='.$result['id'].'/>

the point of the javascript is to assign a value to a hidden field telling the next page what the customer id is for all of checked boxes.

If you the user checks a box that is assigned to a different customer (therefore does not belong in the group) i want to alert the user and then uncheck the last box checked. I can get all the way to the alert but can not uncheck the box the user just checked.

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 trying to add two different companies to the same invoice");
                           /*Uncheck the box just checked by user*/
            }

        else {
            alert("they are the same");
                           /*no modification required*/
            }

}

2 Answers 2

4

You can pass the this object as you call the function:

http://jsfiddle.net/msV24/

HTML

<input type="checkbox" name="inv_add[]" onclick="setcid(\''.$result['cid'].'\',this)" value='.$result['id'].'/>

javascript

function setcid(cid, sender) {

...

    else if (cid !== set_x){
        alert ("You are trying to add two different companies to the same invoice");
                       sender.checked = false;
        }

    else {
        alert("they are the same");
                       /*no modification required*/
        }

}
Sign up to request clarification or add additional context in comments.

Comments

0

I would assign your checkbox and ID of chk_[TheIdOfTheHiddleField]:

<input type="checkbox"
       id='.$result['id'].'
       name="inv_add[]"
       onclick="setcid(\''.$result['cid'].'\')"
       value='.$result['id'].' />

Then un-check it like this:

document.getElementById("chk_" + cid).checked = false;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.