You are here:

Javascript/Javascript Issue Radio Button Validation


Hello, I am creating a form and want to validate some fields. I have written code that works for all my fields but having problem with radio buttons. I have written the following code and it works fine for testing but now where the alerts are, I simply want to "break" out of the if statements. I thought using a "break" would work but apparently you can only use a break statement with a for loop which I don't need. Can you look at the code and tell me if there is a simple solution for this? Thanks!

if (document.getElementById('comments1').checked)
     alert("You selected the comment button");
       else if (document.getElementById('order-status1').checked)
        alert("You selected the order status button");
        else if(document.getElementById('salesinquiry1').checked)
        alert("You selected the sales inquiry button");
         alert("You must select one - Comments, Order Status or Sales Inquiry");

Hi Ron,

Thanks for your question. A simple approach might be to use "document.getElementsByTagName" to  loop through the input elements, check if the type is "radio" and then see if it is checked.

Here is an example of what I am getting at:

I'm sure there are probably a few ways to do this, but this came to mind quickly, and is easy to implement.

Please feel free to follow-up if there are any other questions you may  have. I hope this was helpful to you.

Best Wishes,

Kevin Chisholm


All Answers

Ask Experts


Kevin Chisholm


I can answer questions about: JavaScript, Object-Oriented JavaScipt, JSON, AJAX, jQuery, HTML, CSS


I am a Front-End Web Developer living in New York City. I have provided services to titles such as,,,,,, and Being particularly passionate about Object-Oriented JavaScript as well as Web Standards, I spend a fair amount of time writing about / teaching Front-End Web Development techniques. In my free time I enjoy reading about advanced JavaScript, as well as Web Development trends and best practices. My Blog is: JavaScript-specific blog posts can be found here:

NYC Front-end Developers CSS3 / HTML5 The future of front end Front-End Engineers Javascript Syracuse University Alumni Network

Syracuse University - VPA - 1983 - 1987

Past/Present Clients

©2017 All rights reserved.