All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Get Value Of Radio Button JavaScript

Last Updated : Mar 11, 2024

Get Value Of Radio Button JavaScript

In this article we will show you the solution of get value of radio button JavaScript, in order to select exactly one option from a list, radio buttons are employed.

This tutorial includes certain radio buttons with values that may be selected based on the values, allowing us to modify the HTML document by clicking on the radio button.

And then we will see an example to get the value of the radio button as an alert of the HTML page.

We will be using the .style property to use to add style to HTML elements using JavaScript in another example below.

Step By Step Guide On Get Value Of Radio Button JavaScript :-

Example 1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> get value of radio button javascript </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
    <style>
        h1 {
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
    </style>
</head>
<body>
    <center>
        <h1> TALKERSCODE </h1>
        <h3> get value of radio button javascript </h3>
    </center>
    <form action="">
        <input type="radio" name="car" value="BMW">
        <label for="">BMW</label>
        <br>
        <input type="radio" name="car" value="Ford">
        <label for="">Ford</label>
        <br>
        <input type="radio" name="car" value="KIA">
        <label for="">KIA</label>
        <br>
        <input type="radio" name="car" value="Audi">
        <label for="">Audi</label>
        <br>
        <button onclick="getRadioValue()"> Get radio values </button>
    </form>
    <script>
    function getRadioValue() {
        var radio = document.getElementsByName('car') ;
        for(let i=0; i<radio.length; i++) {
            if(radio[i].checked) {
                alert(radio[i].value) ;
            }
        }
    }
    </script>
</body>
</html>
  1. To write HTML code <! DOCTYPE html> to mention which version of the HTML file is written in.
  2. Then we have to write the <html> tag used to define the root of an HTML document. And also write the ending tag </html>.
  3. <head> tag used here to use to contain the metadata about the HTML file and end it with </head> tag
  4. Then <title> tag is used to set the title of the HTML document and end it with </title> tag.
  5. In order to style the HTML page, we'll utilize an external CSS file
  6. using <style> tag to add CSS
  7. <h1> tag used to add a heading close it with </h1> tag
  8. Create some <input> tags with the type of radio and some values of cars. Give all the radio buttons the same name. The label is added to the same car of value using <label> tag.
  9. Using <br> tag to separate the lines
  10. Create a <button> to get the value with the onclick of a function getRadioValue().
  11. <script> tag is created write JavaScript within it.
  12. First create a function getRadioValue().
  13. Use Let to create a variable radio to get the elements by name by using document.getElementByName()
  14. Now create a for loop and inside if create an if statement. If the radio button the checked then alert the value of the radio button.

Example 2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> get value of radio button JavaScript </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
    <style>
        h1 {
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
    </style>
</head>
<body>
    <center>
        <h1> TALKERSCODE </h1>
        <h3> get value of radio button JavaScript </h3>
    </center>
orange <input type="radio" value="Change Background" onclick="changeBackground()">
    <script>
        function changeBackground() {
            document.body.style.background = "orange";
         }
        </script>
</body>
</html>
  1. To write HTML code <! DOCTYPE html> to mention which version of the HTML file is written in.
  2. Then we have to write the <html> tag used to define the root of an HTML document. And also write the ending tag </html>.
  3. <head> tag used here to use to contain the metadata about the HTML file and end it with </head> tag
  4. Then <title> tag is used to set the title of the HTML document and end it with </title> tag.
  5. In order to style the HTML page, we'll utilize an external CSS file
  6. using <style> tag to add CSS
  7. <h1> tag used to add a heading close it with </h1> tag
  8. <input> tag is created with the type of radio.
  9. <script> tag is created write JavaScript within it.
  10. Create the function changeBackground() at first.
  11. Using .style property to give background and add the value as the input

Conclusion :-

At last, here in conclusion, we can say that with this article’s help, we know How to get value of radio button JavaScript.

I hope this article on get value of radio button JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪