1

I've got a function in JS that check all my checkboxes in a form:

function CheckAll(chk){
    for (i = 0; i < chk.check.length; i++)
        if (chk.All.checked==true){
            chk.check[i].checked = true;
            chk.check[i].disabled = true;
        }
        else {
            chk.check[i].disabled = false;
            chk.check[i].checked = false;
        }
}

I've recently added some radio buttons to my form, and my question is: will this function activate ALL my radio buttons too (I know that only one radio button can be selected, but I'm not sure how this works in a script like that..)?

Here you can find my form code:

<form name='MyForm'>
    <b>This is my Simple Form</b></br>
    </br>
    <input type='radio' name='parameter' value='ONE' checked='checked' />ONE<br/>
    <input type='radio' name='parameter' value='TWO' />TWO<br/>
    <input type='radio' name='parameter' value='THREE' />THREE<br/>
    <input type='radio' name='parameter' value='FOUR' />FOUR<br/>
    <input type='radio' name='parameter' value='FIVE' />FIVE<br/>
    <input type='radio' name='parameter' value='SIX' />SIX<br/>
    <input type='radio' name='parameter' value='SEVEN' />SEVEN<br/>
    <script>
        document.write("</br>");
        var count
        var check_value = new Array()
        check_value[0] = "001"
        check_value[1] = "002"
        check_value[2] = "003"
        check_value[3] = "004"
        for(count in check_value){
            var checkbox = "<input type='checkbox' name='check' value='"+check_value[count]+"' />"
            document.write(checkbox + check_value[count] + "  ");
        }
        document.write("<input type='checkbox' name='All' value='All' onClick='CheckAll(document.MyForm);' /> All");
    </script>
    </br>
    </br>
    <input type=button value='Ok' onClick='foo(document.MyForm);'>
</form>

If this don't work as I want, how can I solve it?

abierto
  • 1,325
  • 6
  • 26
  • 54
  • Did you test it ? If not, why ? – Denys Séguret Jan 11 '13 at 10:23
  • Because at the moment I can't modify a servlet, because I don't have the source code, but only the .class. The foo function will send some parameters to it after a little data elaboration, but before sending them I wanted to know how that works. – abierto Jan 11 '13 at 10:27
  • You cannot check more than one radio so only the last will be activated Also you pass the wrong object. Pass this.form.parameter – mplungjan Jan 11 '13 at 10:27
  • Use `;`. No `document.write()`. Cannot see function call. Ahhhh! – Amberlamps Jan 11 '13 at 10:28
  • @Amberlamps what do you mean? – abierto Jan 11 '13 at 10:30
  • 1
    @abierto: I am sorry. All I was saying is that your code needs a lot of improvement. Use a semicolon `;` at the end of every line, do not use `document.write()`, use literals instead of `new Array()`... This is totally unrelated to your question. I am just saying. – Amberlamps Jan 11 '13 at 10:32
  • @abierto You should put a `;` after your statements. In most case you have no problem omitting them but the rules are complex so put them. – Denys Séguret Jan 11 '13 at 10:33
  • @Amberlamps why I should not use `document.write()` and `new Array()`? – abierto Jan 11 '13 at 10:35
  • 1
    @abieto: Read http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice and http://stackoverflow.com/questions/1094723/what-is-array-literal-notation-in-javascript-and-when-should-you-use-it – Amberlamps Jan 11 '13 at 10:42

3 Answers3

2

Testing is as simple as that : http://jsfiddle.net/yk9uD/

Content :

<form name='MyForm'>
    <b>This is my Simple Form</b></br>
    </br>
    <input type='radio' name='parameter' value='ONE' checked='checked' />ONE<br/>
    <input type='radio' name='parameter' value='TWO' />TWO<br/>
    <input type='radio' name='parameter' value='THREE' />THREE<br/>
    <input type='radio' name='parameter' value='FOUR' />FOUR<br/>
    <input type='radio' name='parameter' value='FIVE' />FIVE<br/>
    <input type='radio' name='parameter' value='SIX' />SIX<br/>
    <input type='radio' name='parameter' value='SEVEN' />SEVEN<br/>
    <script>
      function CheckAll(chk){
  for (i = 0; i < chk.check.length; i++) {
        if (chk.All.checked==true){
            chk.check[i].checked = true;
            chk.check[i].disabled = true;
        }
        else {
            chk.check[i].disabled = false;
            chk.check[i].checked = false;
        }
}
}
        document.write("</br>");
        var count;
        var check_value = new Array()
        check_value[0] = "001"
        check_value[1] = "002"
        check_value[2] = "003"
        check_value[3] = "004"
        for(count in check_value){
            var checkbox = "<input type='checkbox' name='check' value='"+check_value[count]+"' />"
            document.write(checkbox + check_value[count] + "  ");
        }
        document.write("<input type='checkbox' name='All' value='All' onClick='CheckAll(document.MyForm);' /> All");
    </script>
    </br>
    </br>
    <input type=button value='Ok' onClick='foo(some var);'>
</form>

And the answer to the question is : No, it won't select all radio buttons.

The reason is that your code iterates only over inputs whose name is check :

for (i = 0; i < chk.check.length; i++) {
Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
1

No, it won't work for radio buttons. The name given for radio button is different than that of checkbox. So, your function does not touch radio buttons. But if you have created an event for class providing the radio buttons and the checkboxes same class name, the function would also include the radio buttons. But in that case, only the last radio button will be checked. In fact, all radio button gets checked while the code executes. But when the radio button with same name is found again, the previous checked attribute is removed and the checked attribute comes to current node.

hsuk
  • 6,321
  • 12
  • 45
  • 78
0

If you want to check which radio box in checked then you can use following code.

$('.myclass input[name="radio_box_name"]:checked').val())
Unihedron
  • 10,251
  • 13
  • 53
  • 66
viajy
  • 1