0

Inside of a function I'm getting a value from an input box, like this.

    var $value = $('#inputid').val().toLowerCase();

So I want to have the returned variable $value be used to call another variable. Above that code is a variable declaration.

    var $somethingA = "1";
    var $somethingB = "2";

If the user types in "somethingA" I want to get an alert that says "1". Is this possible?

Sorry if it is a silly question. Thanks!

1

1 Answer 1

1

That's possible, but you need to use an object as a map rather than multiple variables to hold your values.

var inputMap = {
    somethingA: 1,
    somethingB: 2
};

var input = $('#inputid').val().toLowerCase();

alert(inputMap[input]);

Note that if you were in the global scope (which would be a bad practice) you could just get the same effect by accesing variables using window[someDynamicProperty], since global variables are properties of the window object in a browser environment.

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

1 Comment

Yes! That's exactly what I wanted, Thanks!

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.