Solved

Javascript Variable Variables

Posted on 2014-10-19
7
101 Views
Last Modified: 2014-10-20
I seem to remember doing this in the past and stuck.
I have variables being created throughout the page like something_1 = one.  I am trying to put two variables together like 'something_' and '1' to return the value of the variable, 'something_1'
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
  <script>
    $(function(){
    var x = 'something_';
    var y = '1';
    var z = x+y
alert(z); // should be 'one' but is 'something_1'

})
  </script>
  <meta charset="utf-8">
  <title>testing</title>
</head>
<body>
  <script>var something_1 = 'one';</script>
  <script>var something_2 = 'two';</script>
</body>
</html>

Open in new window

0
Comment
Question by:Scott Fell,  EE MVE
  • 4
  • 2
7 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40391137
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40391141
else : http://jsfiddle.net/auy1dewL/1/
    $(function(){
        var x = 'something_';
        var y = '1';
        window[x+y] = "10"; // global variable
        alert(something_1); // 10
    });

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 40391145
or : http://jsfiddle.net/auy1dewL/2/
    $(function(){
        var ns = {};
        var x = 'something_';
        var y = '1';
        ns[x+y] = "10";
        alert(ns.something_1); // 10
        alert(ns["something_1"]); // 10
    });

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40391148
something_1="Hello";    
y = '1';
alert(window['something_'+y]);

Open in new window

0
 
LVL 52

Author Closing Comment

by:Scott Fell, EE MVE
ID: 40391196
Thank you Leakim.  I think you missed what I was after.  The variable something_1 was already defined.   You both helped though.

http://jsbin.com/vutaco/1/
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
  <script>
 $(function(){
        var ns = {};
        var x = 'something_';
        var y = '1';
        
        alert(window['something_'+y]);
    });
  </script>
  <meta charset="utf-8">
  <title>testing</title>
</head>
<body>
  <script>var something_1 = 'one';</script>
  <script>var something_2 = 'two';</script>
</body>
</html>

Open in new window

0
 
LVL 52

Author Comment

by:Scott Fell, EE MVE
ID: 40391213
This is the final use case in datatables to dynamically inject a new row

http://live.datatables.net/veqigohe/1/edit
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40391221
this is what I'm showind ID: 40391141
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now