Answers for "w3schools radio button javascript"

55

radio buttons html

<input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
Posted by: Guest on February-09-2020
3

radio button html js

getElementById("radio_button").checked(); //returns boolean
Posted by: Guest on May-16-2021
3

javascript how to select radio button

function check() {
  document.getElementById("red").checked = true;
}
Posted by: Guest on March-04-2021
0

radio button js

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript Radio Buttons</title>
</head>
<body>
    <form>
        <input type="radio" name="choice" value="yes" id="choice-yes"> 
        <label for="choice-yes">Yes</label>
        <input type="radio" name="choice" value="no" id="choice-no">
        <label for="choice-no">No</label>
        <button id="btn">Show Selected Value</button>
    </form>
    <script>
        const btn = document.querySelector('#btn');
        // handle button click
        btn.onclick = function () {
            const rbs = document.querySelectorAll('input[name="choice"]');
            let selectedValue;
            for (const rb of rbs) {
                if (rb.checked) {
                    selectedValue = rb.value;
                    break;
                }
            }
            alert(selectedValue);
        };
    </script>
</body>
</html>Code language: HTML, XML (xml)
Posted by: Guest on September-29-2021

Code answers related to "w3schools radio button javascript"

Browse Popular Code Answers by Language