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,182 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
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
28,779 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
652 views
0 votes
1 answer

How can I display a JavaScript object?

Hello @kartik, Use native JSON.stringify method. Works with nested objects ...READ MORE

answered Aug 28, 2020 in Java-Script by Niroj
• 82,840 points
555 views
0 votes
1 answer

How can I submit a form using JavaScript?

Hello @kartik, Set the name attribute of your form to "theForm" and ...READ MORE

answered Oct 9, 2020 in Java-Script by Niroj
• 82,840 points
672 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,012 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
2,995 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
2,918 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
44,696 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,092 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,829 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