Jump to content

variable as part of a command?


garyed

Recommended Posts

Is there a simple way to use a javascript variable as part of the actual command?

I have var x=3;   & I want my command to be:  document.freeone.t3_home.value="close";    

x is going to change from 1 to 100 so I want to substitute the variable instead of using the actual number 3 in my example. 

Any ideas? 

 

Link to comment
Share on other sites

Javascript allows an object's properties to be accessed using array notation as well, so you can use either document.freeone.t3_home or document.freeone['t3_home'] to access the t3_home property.

Array notation allows the use of variables as the index value, so you can do something like this:

var key = 't3_home';
document.freeone[key].value = 'close';

Now all you have to do is construct the appropriate value for the key variable, which can be done  using simple concatenation.

var x = 3;
var key = 't'+x+'_home';

 

Link to comment
Share on other sites

Thanks for the help,

That would work if "home" stayed the same but it changes too.

I really didn't give a good example of  what I was trying to do because the 3  does need to change but I have a bunch of different values than .."home" also. 

I already have about 60 different fields that I've already set up in a script to get the value I want but only for "t3_home.. t3_wall.. t3_window.. etc... " .

So what I'm trying to do is just put a variable in just where the "3" is so i can run it through a for loop to get t2.. t3.. t4.. etc.       

Link to comment
Share on other sites

15 minutes ago, kicken said:

So make it a variable also and concatenate it as well just like was done with the 3.

That's what I don't know how to do in javascript. I know how to add a variable to a command in php but not javascript. In other words 

var x=3;
document.house.t+x+_home.value="9";

doesn't work.

How can i fit that variable "x" into that command to get it to work the same as:  document.house.t3_home. value="9"; ?

If I can do that then I can figure out the rest of what I want to do.

Link to comment
Share on other sites

5 minutes ago, garyed said:

That's what I don't know how to do in javascript

I showed you everything you need to know to figure out it out my first post.

I showed you how you can replace t3_home with a variable called key.

I showed you how you can construct a variable named key from some strings and another variable containing a number.

Put those together and you have your answer for how to add your x variable into that command.  You can then extrapolate from that and figure out how to make your home part into a variable also.

Link to comment
Share on other sites

21 minutes ago, kicken said:

I showed you everything you need to know to figure out it out my first post.

I showed you how you can replace t3_home with a variable called key.

I showed you how you can construct a variable named key from some strings and another variable containing a number.

Put those together and you have your answer for how to add your x variable into that command.  You can then extrapolate from that and figure out how to make your home part into a variable also.

I do appreciate the help but I still don't think i'm making myself clear. I've tried using the way you showed me with the key variable but in order to make that work i would need 60 different variables. What I need to do is use just a number for the variable so i can run it through a loop without the text. I can't run the way you showed me through a loop because i have to change not just the number inside the key variable but the text inside the variable also. That is why I'm trying to find a way to just add a number variable that can fit into the command line string..   

Link to comment
Share on other sites

41 minutes ago, kicken said:

I showed you everything you need to know to figure out it out my first post.

I showed you how you can replace t3_home with a variable called key.

I showed you how you can construct a variable named key from some strings and another variable containing a number.

Put those together and you have your answer for how to add your x variable into that command.  You can then extrapolate from that and figure out how to make your home part into a variable also.

I must apologize because I finally got it. I didn't realize that i could add to the variable you showed me in the brackets. 

I just used:

for (i=2;i<5;i++) {
var key='t'+i;
 document.tester[key+'_home'].value='';                 
 document.tester[key+'_wall'].value='';
 /// etc..
 }                 
                  

& it works. 

Thanks again 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.