Solved

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

Posted on 2007-11-24
5
750 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
ID: 20344427
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
ID: 20344456
Don't use eval at all:

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

Expert Comment

by:b0lsc0tt
ID: 20344493
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
ID: 20344593
Right... I was looking at the Microsoft documentation, which specifies parentheses...
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20344661
@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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTTPS jquery doesn't work 9 58
Placeholder text not disappearing in Contact Form 7 2 13
how can i extend ? 2 41
Select case on click 3 13
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

808 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