search
HomeWeb Front-endJS TutorialHow to get the selected state of a radio button using JavaScript

To use JavaScript to select the status of a radio button, you need to select the checked attribute of the radio button object. This article introduces how to use JavaScript to obtain the selection status of the radio button.

How to get the selected state of a radio button using JavaScript

Let’s first look at the value of the checked attribute

true: Select the radio button

false: The radio button is not selected

When getting the radio button in the form

The situation specified by the id attribute

Referring to the id form control in JavaScript

document.forms.(参照表格的id).(参照控件的id)

When referencing the selected state of the radio button through the id

document.forms.(参照表格的id).(参照How to get the selected state of a radio button using JavaScript的id).checked

can also be accessed

document.(参照表格的name).(参照How to get the selected state of a radio button using JavaScript的id).checked

When specified by the name attribute

The controls for text boxes and check boxes can be obtained from the name, but for radio buttons, if they are set to the same radio button group , you cannot use Get from Name because the value of Name is the same. We will check the selection status from the ID introduced earlier.

The code is as follows

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title></title>
  <script type="text/javascript" language="javascript">
    function onHow to get the selected state of a radio button using JavaScriptChange() {
      check1 = document.form1.Radio1.checked;
      check2 = document.form1.Radio2.checked;
      check3 = document.form1.Radio3.checked;
      target = document.getElementById("output");
      if (check1 == true) {
        target.innerHTML = "元素1被选中。<br/>";
      }
      else if (check2 == true) {
        target.innerHTML = "元素2被选中。<br/>";
      }
      else if (check3 == true) {
        target.innerHTML = "元素3被选中。<br/>";
      }
    }
  </script>
</head>
<body>
  <form name="form1" action="">
    <input id="Radio1" name="RadioGroup1" type="radio" onchange="onHow to get the selected state of a radio button using JavaScriptChange();" />
      <label for="Radio1">How to get the selected state of a radio button using JavaScript 元素1</label><br/>
    <input id="Radio2" name="RadioGroup1" type="radio" onchange="onHow to get the selected state of a radio button using JavaScriptChange();" />
      <label for="Radio2">How to get the selected state of a radio button using JavaScript 元素2</label><br />
    <input id="Radio3" name="RadioGroup1" type="radio" onchange="onHow to get the selected state of a radio button using JavaScriptChange();" />
      <label for="Radio3">How to get the selected state of a radio button using JavaScript 元素3</label><br />
  </form>
  <div id="output"></div>
</body>
</html>

Explanation:

  <script type="text/javascript" language="javascript">
    function onHow to get the selected state of a radio button using JavaScriptChange() {
      check1 = document.form1.Radio1.checked;
      check2 = document.form1.Radio2.checked;
      check3 = document.form1.Radio3.checked;
      target = document.getElementById("output");
      if (check1 == true) {
        target.innerHTML = "元素1被选中。<br/>";
      }
      else if (check2 == true) {
        target.innerHTML = "元素2被选中。<br/>";
      }
      else if (check3 == true) {
        target.innerHTML = "元素3被选中。<br/>";
      }
    }
  </script>

The above is the JavaScript code to be executed when the selected state of the radio button changes. You can use the checked attribute to get the selected status of a radio button. True if the radio button is checked, false if not. Make sure the attribute is checked and the message is displayed as HTML for the element with the "output" ID.

 <form name="form1" action="">
    <input id="Checkbox1" type="checkbox" /><label for="Checkbox1">单选项目1</label><br />
    <input id="Checkbox2" type="checkbox" /><label for="Checkbox2">单选项目2</label><br />
    <input type="button" value="Exec" onclick="onButtonClick();" />
  </form>

The above is the HTML of the form part. The form has two checkboxes and a button. Execute the onBVuttonCLick() function on the button's onclick event.

Run results

Execute the above HTML file. The effect shown below will be displayed.

How to get the selected state of a radio button using JavaScript

Click the radio button to select it. When selected, a message appears at the bottom of the radio button indicating that the element has been selected.

How to get the selected state of a radio button using JavaScript

In the case of using GetElementById

Using DOM, you can get the selection status of the radio button in GetElemenById, using GetElementById, You can get the radio button even if it doesn't exist in the form.

The code is as follows

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title></title>
  <script type="text/javascript" language="javascript">
    function onHow to get the selected state of a radio button using JavaScriptChange() {
      radiobtn1 = document.getElementById("Radio1");
      radiobtn2 = document.getElementById("Radio2");
      radiobtn3 = document.getElementById("Radio3");

      target = document.getElementById("output");

      if (radiobtn1.checked == true) {
        target.innerHTML = "元素1被选中。<br/>";
      }
      else if (radiobtn2.checked == true) {
        target.innerHTML = "元素2被选中。<br/>";
      }
      else if (radiobtn3.checked == true) {
        target.innerHTML = "元素3被选中。<br/>";
      }
    }
  </script>

</head>
<body>
  <input id="Radio1" name="RadioGroup1" type="radio" onchange="onHow to get the selected state of a radio button using JavaScriptChange();" /><label for="Radio1">How to get the selected state of a radio button using JavaScript 元素1</label><br/>
  <input id="Radio2" name="RadioGroup1" type="radio" onchange="onHow to get the selected state of a radio button using JavaScriptChange();" /><label for="Radio2">How to get the selected state of a radio button using JavaScript 元素2</label><br />
  <input id="Radio3" name="RadioGroup1" type="radio" onchange="onHow to get the selected state of a radio button using JavaScriptChange();" /><label for="Radio3">How to get the selected state of a radio button using JavaScript 元素3</label><br />
  <div id="output"></div>
</body>
</html>

Instructions:

When a radio button is clicked and the selection state is changed, the onHow to get the selected state of a radio button using JavaScriptChange() function will be called. Inside the function, call the GetElementById method and get the radio button objects for Radio 1, Radio 2, Radio 3. The selection state is determined by the value of the checked property of the radio button object. A message corresponding to the selected state of the radio button appears on the page.

Run results

Use a web browser to execute the above HTML file. The effect shown below will be displayed.

How to get the selected state of a radio button using JavaScript

Click the radio button to select. When selected, the selected message will be displayed.

How to get the selected state of a radio button using JavaScript

#If you change the radio button to select, the message content will change accordingly.

The above is the detailed content of How to get the selected state of a radio button using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version