Javascript Form How to - Get RadioButton default Checked state








Question

We would like to know how to get RadioButton default Checked state.

Answer


<!--from www .java 2s .c  o  m-->

<html>
<body>
    <script language="JavaScript">
    function checkBox(){
        if(document.form1.button1.defaultChecked == true){
            console.log("Box1 is checked by default");
        } else if(document.form1.button2.defaultChecked == true){
            console.log("Box 2 is checked by default");
        }
    }
    </script>
    <form name="form1">
        <input type="radio" name=button1>Box 1
        <br>
        <input type="radio" name=button2 CHECKED>Box 2
        <br><br>
        <input type="button" value="Get Default" onClick='checkBox()'>
    </form>
</body>
</html>

The code above is rendered as follows: