Link to home
Start Free TrialLog in
Avatar of sumawuscha
sumawuscha

asked on

How to eval() dynamic object name with '-' in its name

Hello,
i use a form with a lot of checkboxes, which are generated dynamically. Every checkbox has its ID which comes from a database. These IDs can be positive or negative. Thus a checkbox can have a name like "mybox-2".
Sadly my JavaScript function has a problem with such names...
How can i tell the eval() function to evaluate '"document.formWithCheckboxes.mybox" + boxID' as a String for negative boxIDs, too?

Thanks in advance!
function swapAll(boxID) {
            box = eval("document.formWithCheckboxes.mybox" + boxID);
            box.checked = !box.checked;
}

Open in new window

SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You will need to use square brackets with elements.  For example ...

box = document.formWithCheckboxes.elements['mybox' + boxID];

Thanks for the grade, the points and the fun question.

bol
Right... I was looking at the Microsoft documentation, which specifies parentheses...
@GreenGhost - I have made the same mistake at least a few times as I switch from ASP/vbscript to Javascript and back.  The MS document probably was using clientside vbscript which needs parentheses.  Good suggestion to lose the eval() though unless it is needed for some reason we can't see. :)

bol