1
<input type ="checkbox" id = "checkbox1" checked ="checked" value="1" onclick = "a()"/>
<input type ="checkbox" id = "checkbox1" checked ="checked" value="2" onclick = "a()"/>

function(checkbox1,checkbox2)
{ 
   if(checkbox1.checked == true && checkbox2.checked == false)
   {
     alert("checkbox1 is checked, Checkbox2 ix Unchecked");
   }
}

how can i pass the checkbox1 and checkbox2 value in a()???

3 Answers 3

2

You can just grab it using :

var mycheckbox1 = document.getElementById('checkbox1');

and

var mycheckbox2 = document.getElementById('checkbox2');

You might want to change your checkbox2's id to id='checkbox2' having non-unique id is not valid syntax.

If you were asking to modify the function you posted then it's the following:

<input type ="checkbox" id = "checkbox1" checked ="checked" value="1" onclick = "a()"/>
<input type ="checkbox" id = "checkbox2" checked ="checked" value="2" onclick = "a()"/>

function a(){
     var mycheckbox1 = document.getElementById('checkbox1');
     var mycheckbox2 = document.getElementById('checkbox2');
     if(mycheckbox1.checked && !checkbox2.checked)
          alert('checkbox1 is checked, checkbox2 is unchecked');
}

Now if you were asking to get the value of the checkbox that is checked then it's the following:

<input type ="checkbox" id = "checkbox1" checked ="checked" value="1" onclick = "a(this)"/>
<input type ="checkbox" id = "checkbox2" checked ="checked" value="2" onclick = "a(this)"/>

function a(box){
    if(box.checked)
      alert(box.value);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I suppose you could do it this way... If you really wanted to

onclick="a(document.getElementById('checkbox1'), document.getElementById('checkbox2'))"

Comments

0

Here is a solution that gets all checkboxes in the form and displays their value and state

<form id="myForm">
<input type ="checkbox" id = "checkbox1" checked ="checked" value="1" onclick = "a()"/>
<input type ="checkbox" id = "checkbox2" checked ="checked" value="2" onclick = "a()"/>
</form>
<script>
function a(){
    form = document.getElementById("myForm")
    str = ""
for (i=0;i<form.elements.length;i++) {
    type = form.elements[i].type
     if (type=="checkbox") {
            str += "\n"
        str += (form.elements[i].checked)? form.elements[i].id + " is checked " : form.elements[i].id + " is unchecked ";
        str += "\n"
        str +=  form.elements[i].id + " value is  "+ form.elements[i].value;
     }


}
    alert(str);
}
</script>

If you want just individual values /state see below For value

 <input type ="checkbox" id = "checkbox1" checked ="checked" value="1" onclick = "a(this.value)"/>
    <input type ="checkbox" id = "checkbox1" checked ="checked" value="2" onclick = "a(this.value)"/>
    <script>
    function a(v){
        alert(v)
    }
    </script>

For checked/unchecked state

<input type ="checkbox" id = "checkbox1" checked ="checked" value="1" onclick = "a(this.checked)"/>
<input type ="checkbox" id = "checkbox1" checked ="checked" value="2" onclick = "a(this.checked)"/>
<script>
function a(v){
    alert(v)
}
</script>

3 Comments

That will be the value, not the checked state.
The question states "how can I pass the checkbox value"
actually i am not sure what he's asking for now i thought he wanted to do the function he listed, and pass the actual checkbox1 and checkbox2 in.

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.