To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > Misc Help > ClientSide Technologies

ClientSide Technologies Discuss HTML/CSS/Javascript, and any other client-side technologies, here.

Reply
 
Thread Tools Rate Thread Display Modes
Old 05-10-2006, 11:49 PM   #1
gilles007
Junior Member
 
Join Date: Apr 2006
Posts: 13
tricky...how to use a varible in a function to form a variable name?

given a js function with an argument n ie:

myfunction(n)

Is there a way I can use n to refer to a form variable called item_1?

For example, in my function, I want to assign a value to a variable:
document.myform.myinput_1.value = "hello";

I really want the _1 above to be determined by n. That is if 2 is passed, I want:
document.myform.myinput_2.value = "hello";

Thanks for any help!

Gilles
gilles007 is offline   Reply With Quote
Old 05-11-2006, 02:57 AM   #2
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,128
var thisthing=42;

var thatthing= "thisthing";

alert(eval(thatthing));
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 05-11-2006, 09:52 AM   #3
gilles007
Junior Member
 
Join Date: Apr 2006
Posts: 13
Thanks for your suggestion but I 'm still stuck.

It's close though.

The tricky part is that the value of the variable that is passed to my function must be "part of" the final variable name I need to refer to. See my example above.

So how do I somehow make what is passed part of the final variable name I'm constructing?

Thanks,

GIlles
gilles007 is offline   Reply With Quote
Old 05-12-2006, 08:04 AM   #4
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,128
It's just string concatenation...

eval('document.myform.myinput_'+n+'.value = "hello"');
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 05-12-2006, 10:10 AM   #5
gilles007
Junior Member
 
Join Date: Apr 2006
Posts: 13
Of course!

Thanks for your help!

Gilles
gilles007 is offline   Reply With Quote
Old 05-12-2006, 12:22 PM   #6
JPnyc
Administrator
 
Join Date: Jan 2005
Posts: 963
Javascript gives us a native object to deal with this. The elements object

document.myform.elements[i].value =

Then you just change the value of i and iterate through
__________________
PHP builder Online Community Manager

JPnyc is online now   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 04:13 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.