Finding all radio buttons in 'unknown' form

I am trying to write javascript code that will find all radio groups in a form and check to make sure one is clicked in each group. This is not hard if you know the form and group names. What I would like to do is create some code that could be called using 'onsubmit' from any form. I want to keep the code generic enough to use without customization for each form.



During the submit handler the form is available via the this keyword or as the event.currentTarget property. Note that if you are using an inline event handler then it is up to you to pass the appropriate variable to any global function e.g. <form onsubmit="checkRadios(this);">. You can then loop over the form's elements property to look for your radio buttons.


  function onSubmitForSomeForm() {
        var groups = {},
        [], function (input) {
            if (input.type === "radio" && {
                groups[] = groups[] || [];
        for (group in groups) {
            if (!groups[group].some(function (checked) {
                return checked;
            })) {
                return false; //Some group was completely unchecked
        return true;

console.log( "Both groups completely unchecked", document.myform ) );
document.myform.check_list2[0].checked = true;
console.log( "1st group completely unchecked", document.myform ) );
document.myform.check_list2[0].checked = false;
document.myform.check_list[0].checked = true;
console.log( "2nd group completely unchecked", document.myform ) );
document.myform.check_list2[0].checked = true;
console.log( "Both groups have checked checkbox", document.myform ) );

Needs to shim .forEach and .some

To use:

<form onsubmit="return;"></form>


myform.onsubmit = onSubmitForSomeForm


myform.addEventListener( "submit", onSubmitForSomeForm, false );

Get all radio buttons and create an array of the name attributes. Then test if all contain a checked item:

var radios = document.getElementsByTagName("input");
var names = {};
// Iterate over all inputs
for (var i=0; i<radios.length; i++) {
  // Get the radio buttons
  if (radios[i].type = "radio") {
    // Add the name to an object
    if (!names[radios[i].name]) {
      names[radios[i].name] = false;
    // Set the object property to true if this is checked
    if (radios[i].checked) {
      names[radios[i].name] = true;
// Now determine if any group is unchecked (still false)
for (var namegroup in names) {
  if (!names[namegroup]) {
    console.log(namegroup + " isn't checked");

Need Your Help

find inactive gameobject by tag in unity3d

c# unity3d

I have a gameobject which I wish to activate given a certain condition. I gave it a unique tag and I tried using GameObject.FindObjectWithTag("Tag name"). From what I can tell, this method will onl...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.