I have form on page1.php:
<form method="POST" action="page2.php">
<input type="checkbox" name="F10" id="f10">
<input type="checkbox" name="W10" id="w10">
<input type="checkbox" name="F20" id="f20">
<input type="checkbox" name="W20" id="w20">
<input type="checkbox" name="F30" id="f30">
<input type="checkbox" name="W30" id="w30">
</form>
I want to diable a checkbox if another checkbox is checked using javascript or XMLHttpRequest(). I tried doing it using javascript but it didn't work.
if(document.getElementById("f10").checked){
document.getElementById("w20").disabled=true;
}
prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.
When using jQuery and you wish to read whether a checkbox is checked or not. $('#checkboxid').is(':checked'); This will return true if the checkbox is checked and false if left unchecked.
Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.
To check if a checkbox element is checked in TypeScript: Type the element as HTMLInputElement using a type assertion. Use the checked property to see if the element is checked. The property will return true if it is checked and false otherwise.
You can use regular javascript to get a true or false value for checked for example,
var isChecked= document.getElementById('elementName').checked;
if(isChecked){ //checked
//execute code here
}else{ //unchecked
//execute code here
}
or if you want whenever the checkbox check is changed
var checkbox = document.getElementById('elementName');
checkbox.addEventListener("change", functionname, false);
function functionname(){
var isChecked = checkbox.checked;
if(isChecked){ //checked
}else{ //unchecked
}
}
can you try my code? it may help you :D
<form onclick="checkIfChecked()" method="POST" action="page2.php">
<input type="checkbox" name="F10" id="f10">F10
<input type="checkbox" name="W10" id="w10">W10
<input type="checkbox" name="F20" id="f20">F20
<input type="checkbox" name="W20" id="w20">W20
<input type="checkbox" name="F30" id="f30">F30
<input type="checkbox" name="W30" id="w30">W30
</form>
<script>
function checkIfChecked(){
if(document.getElementById("f10").checked){
document.getElementById("w20").disabled=true;
} else {
document.getElementById("w20").disabled=false;
}
}
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With