views:

79

answers:

2

How can i uncheck all radio button using jquery or javascript? The radio buttons i want to uncheck begins with the id=favoritepreferred12333 and the name favorite12333 the numbers 12333 can be different on other radio buttons, but they all begin with id=favoritepreferred{number} and the name=favorite{number} I think i have to use some regex or? Thanks for the Help

+1  A: 

Try this:

var radios = document.getElementsByTagName("input");
for (var i=0; i<radios.length; ++i) {
    if (radios[i].type == "radio" && /^favoritepreferred\d+/.test(radios[i].id) && /^favorite\d+/.test(radios[i].name)) {
        radios[i].checked = false;
        radios[i].removeAttribute("checked");
    }
}
Gumbo
+3  A: 

Well you could add a class to your radio buttons and then select it easily with JQuery.

Alternatively you could use JQuery's attribute-starts-with-selector:

$("input[id^='favorite']").attr("checked", true);
Obalix
Thank you for your help, your version is much shorter +1 :-)
streetparade