0

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

2
  • Can you show some sample HTML for a "checkbox set"? Commented Jun 10, 2010 at 22:35
  • ... and what is optionBoxes? Commented Jun 10, 2010 at 22:37

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

if you think that your code should otherwise work try:

totalLength = optionBoxes[set].length;

Comments

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.