Answers for "js select radio button"

8

html radio button checked

<input type="radio" id="huey" name="drone" value="huey"
         checked> 
<!-- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio-->
Posted by: Guest on May-17-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

Browse Popular Code Answers by Language