1

so i have a checkbox array name "vote[]" and i want to call a function to tick all of them, atm my function is like this

function checkAll(field)  
{  
    for (i = 0; i < field.length; i++)  
        field[i].checked = true ;  
}

and i call it like this

checkAll(document.form.vote)

but it doesnt work... wutdo?

Thanks, Ben

p.s i have tried to doing

checkAll(document.form.vote[]) 

and it doesnt work.

4 Answers 4

3

Try:

checkAll(document.form['vote[]'])

Explanation: [] is part of the name but if you write it like form.vote[], it would be interpreted as JavaScript (probably invalid). Luckily, in JavaScript there are two ways to access object properties: Dot notation, foo.bar, and array notation, foo['bar']. The latter one comes in handy if the property is not a valid JavaScript identifier.

Further suggestion: As we don't know how your HTML look like document.form might also not work. I suggest to give the form an ID and call:

checkAll(document.getElementById('yourFormID')['vote[]'])

Update:

Works for me: DEMO

7
  • Doesnt Work :S any other ideas? Commented Jan 6, 2011 at 22:52
  • tried both and neither worked :S my html is simple ill copy both the button and the javascript Commented Jan 6, 2011 at 22:55
  • <input type="button" name="selectall" value="Select All" class="btn" OnClick="checkAll(document.getElementById("form")["vote[]"]) " /> Commented Jan 6, 2011 at 22:55
  • function checkAll(field) { for (i = 0; i < field.length; i++) field[i].checked = true ; } Commented Jan 6, 2011 at 22:55
  • @Ben Mills: You have a quoting error. Look at it precisely: OnClick="checkAll(document.getElementById("form")["vote[]"])". You cannot use double quotes inside a double quoted string. Use single quotes instead: OnClick="checkAll(document.getElementById('form')['vote[]'])" . Have a look at the demo. Commented Jan 6, 2011 at 22:59
2

Although the original poster was able to get this working, another way (avoiding the getElementsByTagName call) would be:

checkAll(document.formname.elements['vote[]'])

Hope that helps someone in the future. :)

2

Try doing this with by filtering with checkbox only i.e type='checkbox'

var boxes = document.getElementsByTagName('input');
    if(boxes.type='Checkbox'){
        for (var i = 0; i < boxes.length; i++) {
            if (boxes[i].name == 'vote[]' ) {
                boxes[i].checked = true;
            }
        }
    }
0

Try doing this with document.getElementsByName instead:

checkAll(document.getElementsByName('vote[]'));

If that doesn't work, have a go with this, looping through all input elements.

var boxes = document.getElementsByTagName('input');
for (var i = 0; i < boxes.length; i++) {
    if (boxes[i].name == 'vote[]') {
        boxes[i].checked = true;
    }
}
1
  • Doesnt Work :S any other ideas? Commented Jan 6, 2011 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.