Solved

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

Posted on 2007-11-24
5
757 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Web Development 6 68
Table header must be on top 2 57
allow link tag to be submit like form 15 60
How to confirm and grey the submit button using JavaScript ? 14 67
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

734 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