473,394 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Javascript string to variable

CroCrew
564 Expert 512MB
Hello Everyone,

First let me say thanks for the help. Now for the question…

What I am trying to do is have a few declared variables where their names share the same common first part and have a unique second part. Then within a function I pass in the second part of the variable’s name and concatenate that with the first part to alert the value of that variable. Below is my code.

Again thanks for your help~


[HTML]<script language="JavaScript">
var LocalOne=’Hello’;
var LocalTwo=’Wow’;
var LocalThree=’Bye’;

function ShowVar(SecondPart)
{
alert(['Local'+SecondPart].value);
}
</script>[/HTML]
Jan 24 '08 #1
4 14938
gits
5,390 Expert Mod 4TB
hi ...

have a look at the following example ... it assumes that the vars are global:

Expand|Select|Wrap|Line Numbers
  1. var LocalOne = 'Hello';
  2. var LocalTwo = 'Wow';
  3. var LocalThree = 'Bye';
  4.  
  5. function ShowVar(SecondPart) {
  6.     alert(window['Local' + SecondPart]);
  7. }
  8.  
  9. ShowVar('One');
kind regards
Jan 24 '08 #2
CroCrew
564 Expert 512MB
Can I also set a new value to the variable using..

window['Local' + SecondPart] = “Cool”;


Thanks~
Jan 24 '08 #3
CroCrew
564 Expert 512MB
Like this?

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2.      var LocalOne=’Hello’;
  3.      var LocalTwo=’Wow’;
  4.      var LocalThree=’Bye’;
  5.  
  6.      function ShowVar(SecondPart)
  7.      {
  8.           alert(window['Local' + SecondPart]);
  9.           window['Local' + SecondPart] = 'Cool';
  10.      }
  11. </script>
  12.  
Jan 24 '08 #4
gits
5,390 Expert Mod 4TB
you answered your question already for yourself :) that works:

Expand|Select|Wrap|Line Numbers
  1. var LocalOne = 'Hello';
  2. var LocalTwo = 'Wow';
  3. var LocalThree = 'Bye';
  4.  
  5. function ShowVar(SecondPart) {
  6.     window['Local' + SecondPart] = 'test';
  7.     alert(window['Local' + SecondPart]);
  8. }
  9.  
  10. ShowVar('One');
  11.  
  12. // to see for sure that it works:
  13. alert(LocalOne);
  14.  
kind regards
Jan 24 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: PK9 | last post by:
I have a string variable that holds the equivalent of a DateTime value. I pulled this datetime from the database and I want to strip off the time portion before displaying to the user. I am...
1
by: Martin Heal | last post by:
I am having problems trying to assign the contents of a Memo table field to a String variable in VB using the following code: Private Function udfBuildSQL(SQLID As Integer) As Variant Dim rs...
3
by: XenofeX | last post by:
How can i call to object from string variable ? For instance string x = "textBox1"; i want to call the object which name is stored in the x string variable. The most important thing is the...
1
by: Machi | last post by:
let say i have a web form with a button on it ... when i click the button, it will pop up one small window to allow me to save a pdf file, everything is done using code behind as below ... however,...
4
by: JohnR | last post by:
Hi all, I'm finally sick and tired of manually generating get/set properties for each private variable in a class so I'm trying to create a macro to do it. I'm stuck because I can't figure out...
8
by: Pavils Jurjans | last post by:
Hello, I have been developing an Ajax-style framework for couple of years now. Now I am reworking some parts of it. The problem was that I used to use JSON for JavaScript value...
6
by: John Kotuby | last post by:
Hi all, I am using a 3rd party program in a VS2005 web project. The tool takes as input a string containing HTML and converts it to RTF. I have been creating a page by dynamically loading...
2
by: Looch | last post by:
All, I'm trying to output but I can only get (brackets for clarity) when using the code below. How can I "break" into the query variable in the InsertName method to add the name parameter to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.