Solved

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

Posted on 2007-11-24
5
744 Views
Last Modified: 2008-02-01
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

0
Comment
Question by:sumawuscha
  • 3
  • 2
5 Comments
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 60 total points
Comment Utility
sumawuscha,

Try ...

box = eval("document.formWithCheckboxes.elements[mybox" + boxID + "]");

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 65 total points
Comment Utility
Don't use eval at all:

box = document.formWithCheckboxes.elements('mybox' + boxID);
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
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
0
 
LVL 29

Expert Comment

by:Göran Andersson
Comment Utility
Right... I was looking at the Microsoft documentation, which specifies parentheses...
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
@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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now