Solved

Javascript Variable Variables

Posted on 2014-10-19
7
106 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
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 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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Geolocation works...sometimes 2 39
Diff of the day 2 42
JQuery Data Table Default Sort 2 32
How to confirm and grey the submit button using JavaScript ? 14 67
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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)

739 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