garyed Posted October 1, 2020 Share Posted October 1, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/311552-variable-as-part-of-a-command/ Share on other sites More sharing options...
kicken Posted October 1, 2020 Share Posted October 1, 2020 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'; Quote Link to comment https://forums.phpfreaks.com/topic/311552-variable-as-part-of-a-command/#findComment-1581676 Share on other sites More sharing options...
garyed Posted October 1, 2020 Author Share Posted October 1, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311552-variable-as-part-of-a-command/#findComment-1581677 Share on other sites More sharing options...
kicken Posted October 1, 2020 Share Posted October 1, 2020 25 minutes ago, garyed said: That would work if "home" stayed the same but it changes too. So make it a variable also and concatenate it as well just like was done with the 3. Quote Link to comment https://forums.phpfreaks.com/topic/311552-variable-as-part-of-a-command/#findComment-1581679 Share on other sites More sharing options...
garyed Posted October 1, 2020 Author Share Posted October 1, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311552-variable-as-part-of-a-command/#findComment-1581681 Share on other sites More sharing options...
kicken Posted October 1, 2020 Share Posted October 1, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311552-variable-as-part-of-a-command/#findComment-1581682 Share on other sites More sharing options...
garyed Posted October 1, 2020 Author Share Posted October 1, 2020 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.. Quote Link to comment https://forums.phpfreaks.com/topic/311552-variable-as-part-of-a-command/#findComment-1581684 Share on other sites More sharing options...
garyed Posted October 1, 2020 Author Share Posted October 1, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/311552-variable-as-part-of-a-command/#findComment-1581685 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.