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.

Thanks

Answers


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.


Demo: http://jsfiddle.net/rBaUM/16/

  function onSubmitForSomeForm() {
        var groups = {},
            group;
        [].forEach.call(this.elements, function (input) {
            if (input.type === "radio" && input.name) {
                groups[input.name] = groups[input.name] || [];
                groups[input.name].push(input.checked);
            }
        });
        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", onSubmitForSomeForm.call( document.myform ) );
document.myform.check_list2[0].checked = true;
console.log( "1st group completely unchecked", onSubmitForSomeForm.call( document.myform ) );
document.myform.check_list2[0].checked = false;
document.myform.check_list[0].checked = true;
console.log( "2nd group completely unchecked", onSubmitForSomeForm.call( document.myform ) );
document.myform.check_list2[0].checked = true;
console.log( "Both groups have checked checkbox", onSubmitForSomeForm.call( document.myform ) );

Needs to shim .forEach and .some

To use:

<form onsubmit="return onSubmitForSomeForm.call(this);"></form>

Or:

myform.onsubmit = onSubmitForSomeForm

Or:

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.