Solved

Javascript Variable Variables

Posted on 2014-10-19
7
103 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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)

932 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

9 Experts available now in Live!

Get 1:1 Help Now