1

I have defined a variables outside of a function and I need to be able to call the variable depending on the parameter that is called with the function.

HTML

<button id="Area300" style="left:755px;" onclick="a('normal', 'Area300')"></button>

JAVASCRIPT

var Area300_Data = "somedata"
function a(data, data1) {
  if (data=='extra') {
    //do something
  } else if (data=='normal') {
    console.log(data1+_Data)
    //Should display "somedata"
  }
}

How do I combine the parameter of data1 to _Data to get (in this case) Area300_Data?

1
  • use object key value pair Commented Feb 18, 2019 at 10:57

6 Answers 6

3

That may not work unless you use eval. Secondly _Data will act as a variable unless you add it as a string. So either put it inside quotes or use template literals

var Area300_Data = "somedata"

function a(data, data1) {
  if (data == 'extra') {
    //do something
  } else if (data == 'normal') {
    console.log(eval(`${data1}_Data`))
    //Should display "somedata"
  }
}
<button id="Area300" style="left:755px;" onclick="a('normal', 'Area300')" type='button'>Click</button>

Sign up to request clarification or add additional context in comments.

2 Comments

Actually you don't need eval here. var variables are stored into the window object. Except if the code is already into a function context, but it doesn't seem to be the case here.
I have var Area300_Date defined in a window.onload function (as the data for the variable is from a JSON file). I get the error Area300_Data is not defined at eval How do I correct this?
2

If you are in the global scope, every var are stored into window

So you can

window[`${data1}_Data`]

Comments

0

You can keep the data in Object and access that data like property

e.g

var data = {
 Area300_Data : "somedata"
}

Now you can access that data dynamically like this

function a(data, data1) {
  if (data=='extra') {
    //do something
  } else if (data=='normal') {
    console.log(data[data1+'_Data'])
    //Should display "somedata"
  }
}

It will be more readable than accessing window object

Comments

0

Why don't you try to create an object which uses data1 as a key may be something like that?

var gobalInfo = {
   Area300: "somedata"
}
function a(data, data1) {
  if (data=='extra') {
    //do something
  } else if (data=='normal') {
    console.log(globalInfo[data1]);
    //Should display "somedata"
  }
}

Comments

0

data1+_Data apply + operator between two variables named data1 and _Data. You can't access the variable like.
In this case Area300_Data is in global scope and declared using var so its present in window. You you access it from window using Bracket Notation []

var Area300_Data = "somedata"
function a(data, data1) {
  if (data=='extra') {
    //do something
  } else if (data=='normal') {
    console.log(window[`${data1}_Data`])
    //Should display "somedata"
  }
}
<button id="Area300" style="left:755px;" onclick="a('normal', 'Area300')">button</button>

However I would prefer to keep the data in and object named Data and access it from there

let Data = {
   Area300:"somedata"
}
var Area300_Data = "somedata"
function a(data, data1) {
  if (data=='extra') {
    //do something
  } else if (data=='normal') {
    console.log(Data[data1])
    //Should display "somedata"
  }
}
<button id="Area300" style="left:755px;" onclick="a('normal', 'Area300')">button</button>

Comments

0

simply use eval to get variable data. for example

var Area300_Data = "300 Area Data";
var Area600_Data = "600 Area Data";

function a(data, data1) {
  if (data == 'extra') {
    //do something
  } else if (data == 'normal') {
    console.log(eval(`${data1}_Data`))
    //Should display "somedata"
  }
}
<button onclick="a('normal','Area300')">Click to see Area300</button>
<button onclick="a('normal','Area600')">Click to see Area600</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.