Javascript Form How to - Check with RadioButton is default checked








Question

We would like to know how to check with RadioButton is default checked.

Answer


    <!--  w w  w  . j  av  a  2  s.  c o m-->
<html>
<body>
<script language="JavaScript">
function function1() {
    if (myRadioButton1.defaultChecked) {
        console.log("Radio 1 is checked");
    }
    if (myRadioButton2.defaultChecked) {
        console.log("Radio 2 is checked");
    }
    if (myCheckBox.defaultChecked) {
        console.log("Checkbox is checked");
    } 
} 
</script>
    <input id="myRadioButton1" type="radio" checked value="radiobutton">Radio 1<br>
    <input id="myRadioButton2" type="radio" value="radiobutton">Radio 2<br>
    <input id="myCheckBox" type="checkbox" value="checkbox" checked>Checkbox</p>
    <input type="button" value='Click here' onclick="function1();">
</body>
</html>

The code above is rendered as follows: