views:

32

answers:

2

Hey all,

I'm pretty new to Javascript, so forgive me if this is a simple question.

I'm trying to access the length of a set of checkboxes in a form using Javascript. However, I need to be able to change the "name" field of the checkboxes to check several different sets of them. Right now, my sample code looks like:

var set = "set" + x;
totalLength = optionBoxes.set.length;

The variable x is being incremented by a for loop that wraps the whole thing and the name of the checkbox sets that I'm trying to access are set0, set1, set2, etc.

Thanks.

Edit: small typo fixes

+1  A: 

Probably you want this:

var set = "set" + x;
totalLength = optionBoxes[set].length;

In Javascript, properties of an object are usually accessed as object.name, but they can also be accessed by object["name"] if you have the name as a string.

sth
Thank you! I knew it was that simple. I'm so new I can't accept answers yet, but will do so as soon as I'm allowed.
CoV
A: 

if you think that your code should otherwise work try:

totalLength = optionBoxes[set].length;

unomi