How to get radio button value on click?

How to get the radio button value on click?


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Javascript Radio Button</title>
</head>

<body>
    <input type="radio" name="fav" value="one" id="one"> One
    <input type="radio" name="fav" value="two" id="two"> Two
    <input type="radio" name="fav" value="three" id="three"> Three
    <input type="radio" name="fav" value="four" id="four"> Four
    <div> You select : </div>

    <div id="checked"> </div>
    <script>
        const radios = document.getElementsByName("fav");
        const checked = document.getElementById("checked")

        radios.forEach(item => {
            item.addEventListener("input", () => {
                checked.innerText = item.value
            })
        })
    </script>
</body>

</html>
How to get radio button value on click?

Reactions

Post a Comment

0 Comments