September 17, 2021
How to Get Value from Radio Button in Javascript
In this video tutorial, you will learn how to get value from radio button in javascript. If you are having radio buttons in your HTML file, then it is obvious you want to get the value of the selected radio button and do something with it.
As you already know, in a particular group of radio buttons, you can only select one radio button. A good example of using a radio button would be in a signup form where a user can select the gender.
In the following example, we have multiple radio buttons. I will select one radio button randomly and upon click of a button, I will display its value or label text on the screen. Please have a look over the code example and steps given below.
HTML & CSS
- We have a few elements in the HTML file and that includes
div
,label
,button
, andh1
. - The
div
element is just a wrapper for the rest of the elements. - We have created multiple radio buttons using the
input
element. - The inner text for the
button
element is“Show”
and for theh1
element is“Result”
. - We have done some basic styling using CSS and added the link to our
style.css
stylesheet inside thehead
element. - We have also included our javascript file
script.js
with ascript
tag at the bottom.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" href="style.css"> <title>Document</title> </head> <body> <div class="container"> <label><input type="radio" name="username" value="peter">Peter</label> <label><input type="radio" name="username" value="marks">Marks</label> <label><input type="radio" name="username" value="james">James</label> <label><input type="radio" name="username" value="mary">Mary</label> <button>Show</button> <h1>Result</h1> </div> <script src="script.js"></script> </body> </html>
body { text-align: center; } .container { display: inline-block; } button { display: block; margin: 20px auto; }
Javascript
- We have selected
button
element andh1
element usingdocument.querySelector()
and stored them inbtnShow
andresult
variables respectively. - We have attached the
click
event to thebutton
element. - We are using CSS selector
input[type="radio"]:checked
to get selected radio button and storing it inselected
variable. - As you can see each
input
element is wrapped with alabel
element. That means thelabel
element is the parent of theinput
element. In javascript, each element hasparentElement
property which we can use to get the parent element. Similarly, we can get text content using thetextContent
property. We are using both of them to get the label text of the selected radio button. - You can also get the value of the selected radio button by using the
value
property.
let btnShow = document.querySelector('button'); let result = document.querySelector('h1'); btnShow.addEventListener('click', () =>{ let selected = document.querySelector('input[type="radio"]:checked'); result.innerText = selected.parentElement.textContent; });