How To Get The Value Of A Selected Radio Button

To get the value of a selected radio button in JavaScript, you can use the querySelector method to select the checked radio button and then access its value property. Here’s an example:


// JavaScript
const radioButton = document.querySelector('input[name="color"]:checked');
const selectedValue = radioButton.value;
console.log(selectedValue); // Output: the value of the selected radio button

In the above example, the querySelector method is used with the CSS selector "input[name="color"]:checked" to select the checked radio button with the name attribute set to "color". The value property of the selected radio button is then accessed to get its value.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!