Solved

Javascript Variable Variables

Posted on 2014-10-19
7
107 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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 53

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 53

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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)
Suggested Courses

622 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