2
<input type="radio" checked="checked" value="true" name="child">
<span class="label">Show</span>
<input type="radio" class="hide" value="false" name="child">
<span class="label">Hide</span>

and another radio button on the same page

<input type="radio" checked="checked" value="true" name="baby">
<span class="label">Show</span>
<input type="radio" class="hide" value="false" name="baby">
<span class="label">Hide</span>

I need to know if all the radio buttons having value="false" are checked using javascript.

Note: The radio button names are different

Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
vetri02
  • 3,199
  • 8
  • 32
  • 43

3 Answers3

2

If you use jQuery, it's very clean:

if ($("input[type='radio'][value='false']").not(':selected').length==0) {
  // Do what you want
}

The jQuery expression means all the false radio buttons are selected

Jens Roland
  • 27,450
  • 14
  • 82
  • 104
2

boudou beat me by 35 seconds...

However, besides the for you can use a foreach logic:

var buttons = document.getElementsByTagName("input");

for (var i = 0; i < buttons.length; i++) {
    var button = buttons[i];
    var id = button.getAttribute("id");
    var type = button.getAttribute("type");
    var value = button.getAttribute("value");
    var checked = button.getAttribute("checked");
    if (type === "radio" && value === "false" && checked === "checked") {
        alert(id);
    }
}

The script can be condensed, it's written like this to allow a better understanding.

See a demo here.

Albireo
  • 10,977
  • 13
  • 62
  • 96
  • Consider the +1 a -1, vote got locked. See http://stackoverflow.com/a/243778/247702 for why you shouldn't use a `for in` loop. – user247702 Jun 04 '12 at 11:40
1

You can try this:

function testRadios() {
    var r = document.getElementsByTagName("input");

    for (var i=0; i < r.length; i++) {
        if (   (r[i].type == "radio")
            && (r[i].value == "false")
            && (r[i].checked != "checked")) {
            return false;
    }
    return true;
}

PS: Use <label> instead of <span class="label">.

bfontaine
  • 18,169
  • 13
  • 73
  • 107