-1

Sorry this isn't easy to explain..

When my script loads it reads some values to variables. eg:

enabled = $("#enabled").prop("checked") ? '1' : '';
local = $("#local").prop("checked") ? '1' : '';
senior = $("#senior").prop("checked") ? '1' : '';

Later in the script if a user has clicked on an element, I want to check if the selected element exists and is 1 or not.

$("body").on('change', '#enabled, #local, #senior', function() {
    var Check = $(this).attr('id')

Check returns enabled, local or senior.

How can I use that to get the value from the relevant variable called enabled, local or senior ?

Thanks

Tom
  • 1,436
  • 24
  • 50
  • Are enabled, local and senior are global (window) variables? – Krypt1 May 18 '18 at 11:31
  • Possible duplicate of [How to find JavaScript variable by its name](https://stackoverflow.com/questions/724857/how-to-find-javascript-variable-by-its-name) – CBroe May 18 '18 at 11:39

1 Answers1

1

You can access variable value by passing variable name to window object:

window[Check]
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125