

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to select a radio button by default in JavaScript?
To select a radio button by default we have to check it as true. If it is checked as true then by default it will be autofocused.
In the following example, we have only radio buttons and none of them is autofocused.
Example
<html> <form id="radiobuttons" name="radiobutton"> <input id="rad1" value="a" type="radio" name="check"/>male <input id="rad2" value="b" type="radio" name="check"/>female <input id="rad3" value="c" type="radio" name="check"/>other </form> <body> </body> </html>
When the above code is executed we will get radio buttons and none of them is autofocused as shown in the following image.
Output
To autofocus any of the above buttons we have to check them initially. This can be done by using pure javascript as shown in the following example.
Example
<html> <form id="radiobuttons" name="radiobutton"> <input id="rad1" value="a" type="radio" name="check"/>male <input id="rad2" value="b" type="radio" name="check"/>female <input id="rad3" value="c" type="radio" name="check"/>other </form> <body> <script> radiobtn = document.getElementById("rad1"); radiobtn.checked = true; </script> </body> </html>
After executing the above code, we will get the following output.
Output
- Related Questions & Answers
- Using Selenium in Python to click/select a radio button.
- How to select a radio button in a page in Selenium with python?
- How to find a radio button element by value using Selenium?
- How to create a Radio Button using JavaFX?
- How to use Radio button in Android?
- Bootstrap Form Radio Button
- How to use Radio Button in Android Kotlin?
- How to set OnclickListener on a radio button in android?
- How can I send radio button value in PHP using JavaScript?
- How to add radio button list in alert dialog?
- How to change my code to uncheck radio button from JavaScript to jQuery?
- JavaFX example to add a tooltip to a radio button
- Create a default Bootstrap button
- Checking a radio in radio group with JavaScript?
- How to pass Radio Button Data to Python CGI script?
Advertisements