How can I check whether a radio button is selected with JavaScript

0 votes
I have two radio buttons within an HTML form. A dialog box appears when one of the fields is null. How can I check whether a radio button is selected?
Sep 3, 2020 in Java-Script by kartik
• 37,520 points
2,223 views

1 answer to this question.

0 votes

Hello @kartik,

You can use vanilla JavaScript way

var radios = document.getElementsByTagName('input');
var value;
for (var i = 0; i < radios.length; i++) {
    if (radios[i].type === 'radio' && radios[i].checked) {
        // get value, set checked flag or do whatever you need to
        value = radios[i].value;       
    }
}

Hope it helps!!
Thank you!!

answered Sep 3, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
0 answers

How do I check if a variable is undefined in JavaScript?

I am facing a problem in js ...READ MORE

3 days ago in Java-Script by Ashutosh
• 12,620 points
18 views
0 votes
0 answers
0 votes
2 answers

How can I set a session var using javascript and get it via php code?

Yes it is possible. You can either ...READ MORE

answered Aug 9, 2020 in Java-Script by Okugbe
• 280 points
29,009 views
0 votes
1 answer

How can I determine if a variable is 'undefined' or 'null'?

Hello @kartik, You can use the qualities of ...READ MORE

answered Aug 28, 2020 in Java-Script by Niroj
• 82,840 points
696 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,190 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
3,049 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
3,007 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
45,085 views
0 votes
1 answer

How can I check if a checkbox is checked?

Hello @kartik, Try this: function validate() { var ...READ MORE

answered Oct 8, 2020 in Java-Script by Niroj
• 82,840 points
1,132 views
0 votes
1 answer

How can I send a message to a particular client with socket.io?

Hii, You can try the code below: io.to(socket.id).emit("event", data); whenever ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,840 points
27,895 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP