0

I have a JavaScript array declared in my JavaScript page like so:

var example = [[20,870,5,1,1,16],[28,1250,8,1,1,23]];

This works fine when accessed elsewhere.

I have a checkbox in my HTML page like so:

<input type="checkbox" id="example" onChange="update('example')">Example

I want the function to look something like below. The variable being passed is also the name of my JS array. I will have 20-30 check boxes and I would ideally like to avoid a load of if/else statements.

function update(a) {

alert(a[0][1]);

}

Is there anyway to use the variable passed into my function to access the namesake JS array?

Thanks

1
  • 1
    You mean like update(example) ? This is very bad code design though. Commented Apr 23, 2016 at 2:40

2 Answers 2

1

If example is defined, you can remove quotes surrounding parameter example at update(example) call

<script>
  var example = [
    [20, 870, 5, 1, 1, 16],
    [28, 1250, 8, 1, 1, 23]
  ];

  function update(a) {
    alert(a[0][1]);
  }
</script>

<input type="checkbox" id="example" onchange="update(example)">Example

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

Comments

0

Try

function update(a) {
    alert(window[a][0][1]);
}

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.