0

I want to get the data from the Variable "EDDBGnd". the debug Paragraph should display "This is a test".

var Apt = "EDDB";
var Menu = "Gnd";
var EDDBGnd = "This is a test";

document.getElementById("debug").innerHTML = Apt.concat(Menu);
customcommander
  • 17,580
  • 5
  • 58
  • 84
aplayz
  • 9
  • 3
  • You could try `window[Apt + Menu]`, assuming `EDDBGnd` is in the global/`window` scope. – gen_Eric Jan 25 '21 at 19:11
  • A better option would be to use an object to hold the `EDDBGnd` data, like `var data = {EDDBGnd: "This is a test"};` then you could do `data[Apt + Menu]`. – gen_Eric Jan 25 '21 at 19:12
  • 1
    See also: [“Variable” variables in Javascript?](https://stackoverflow.com/questions/5187530/variable-variables-in-javascript) – 3limin4t0r Jan 25 '21 at 19:23

4 Answers4

1

If you have the ability to use a different method to store the data, I would recommend using an object instead. This will allow you to have the apt as a key that can hold multiple menu's for example.

It is more complex, but it will allow you to easily grow the object for more data without messing with variables.

var data = {
  "EDDB": {
    "Gnd": "Test is a test",
    "Gnd2": "Test is also a test",
  }
}

document.getElementById("debug").innerHTML = data["EDDB"]["Gnd"];
document.getElementById("debug2").innerHTML = data["EDDB"]["Gnd2"];
<div id="debug"></div>
<div id="debug2"></div>
imvain2
  • 15,480
  • 1
  • 16
  • 21
  • Have not tryed it, but it should work, but It would sadly not work with my array data structure Somebody else posted something that works; Thank you – aplayz Jan 25 '21 at 20:45
1

If EDDBGnd is a global variable you can write

document.getElementById("debug").innerHTML = window[Apt+Menu];
Kshitij
  • 649
  • 5
  • 8
0

Try something like that:

document.getElementById("debug").elements[0].value = EDDBGnd;
arevilla009
  • 429
  • 3
  • 18
0

You need to scope the global variables to the window object.

var Apt = "EDDB";
var Menu = "Gnd";
var EDDBGnd = "This is a test";

document.getElementById("debug").innerHTML =
  window[window.Apt.concat(window.Menu)];
<div id="debug"></div>

A better way is to create a context or localization file (or object).

const context = {
  'EDDB' : {
    'Gnd': 'This is a test'
  }
};

const Apt = 'EDDB'; // From GUI?
const Menu = 'Gnd'; // From GUI?

const getText = (Apt, Menu) => context[Apt][Menu];

document.getElementById("debug").innerHTML = getText('EDDB', 'Gnd');
<div id="debug"></div>
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132