Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable problem 1

Status
Not open for further replies.

dle46163

IS-IT--Management
Jul 9, 2004
81
US
Ok, I think this will be an easy one...

I'm piecing together a variable like this:
cb=ikey+"_cb";

This variable ends up being the name of some checkboxes in a form. I want to place the variable in the following alert statement but it won't work!

alert(schedule.cb.length);

If I type in the real name of the form element like:
alert(schedule.145_cb.length);
it does work... So I'm clearly not using the correct syntax as the Javascript is using a literal CB instead of the variable CB. Any thoughts?

 
Something like this help you ?

Code:
alert( eval( "schedule." + cb + ".length" ) );

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Or you could avoid the unnecessary use of eval and go for this method instead:

Code:
var cbNum = 145;
alert(document.forms['schedule'].elements[cbNum + '_cb'].length);

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Question for Dan:

Is the reason for avoiding eval() because the value is already in an array element and a function call is unnecessary or is the there a performance issue with eval()?



Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
The latter - because of the generic and uncertain nature of the input to eval (from the JS engine's point of view), it is much quicker to avoid the use of it if a more specific method of getting your result is available.



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top