Solved

Javascript Variable Variables

Posted on 2014-10-19
7
104 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

777 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