Get variable by variable [duplicate] - javascript

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 1 year ago.
let varone = "vartwo";
let vartwo = "answer";
And I would like to get "answer". But I cannot do the following:
console.log(vartwo);
How can I get "answer", but not by including "vartwo" in my code?

I think one way to resolve it, is by using javascript objects, as shown:
obj={
varone:"vartwo",
vartwo:"answer"
};
console.log(obj[varone]) //outputs "answer"

Related

Jquery: variable declared and get variable after split [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
jQuery : how to access the name of the element [duplicate]
(3 answers)
Closed 1 year ago.
I have 2 variables.
var a = 1;
var b = 2;
Now I have 2 input boxes with name 'form-1-a' and 'form-1-b'.
I need to put variables in these input boxes. Please look at below code as it is not working.
$("input[name^='form-']").val(eval($(this).attr('name').split('-')[2]));
Though entire code is bit complex but simplified and it does not seem to work without any error message.
Javascript is fun and pain at same time.
Below code worked.
$("input[name^='form-']").each(function(){
$(this).val(eval($(this).attr('name').split('-')[2]))
});

How to assign a attribute value to a variable? [duplicate]

This question already has answers here:
JavaScript Dynamic Variable Names [duplicate]
(6 answers)
Closed 6 years ago.
Is it possible to use button attribute as a variable name of an array in JavaScript? I tried to do it as follows but it returns syntax error.
var $(this).attr("value") = [];
Is there any way to do this?
var buttonValueAttribute = $(this).attr('value');

Double String Variable Name (Javascript) [duplicate]

This question already has answers here:
JavaScript Dynamic Variable Names [duplicate]
(6 answers)
Convert string to variable name in JavaScript
(11 answers)
Closed 7 years ago.
I know there are other questions similar to this, but I feel as if they don't answer my question. I would basically like to make Dynamic Variable Names. Let me give you an example:
//Take the strings "new" and "variable" and make them into a variable.
"new" + "variable" = "Hello World";
//So technically the variable name is "newvariable" and has the value "Hello World"
So basically it takes two strings, and combines them into one variable name. How would I go about doing this?
P.S. This is NOT to combine the values of variables, just the names
Write it into an array or object
var arr = {};
arr['new' + 'variable'] = 'Hello World';
then
alert(arr['newvariable']);
or
alert(arr.newvariable);

How insert variable in JS [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 9 years ago.
I need help, how to insert variable n as XX. Variable n is number, for example 3 and I need to get this number to document.form1.any3.value; is it possible? Thanks for advice
function pokus(n){
var any1 = document.form1.anyXX.value;
}
Use square bracket notation:
function pokus(n){
var any1 = document.form1['any'+n].value;
}

Reference a value in JavaScript [duplicate]

This question already has answers here:
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
Closed 9 years ago.
I have a couple of check boxes that I'd like to use their "name" attribute as the value I reference within my array.
Let's say I have an array like so:
var test = {
"one" : 1,
"two" : 2,
}
var someBtn = "one";
How can I say the following?
test.someBtn; // Which would translate to test.one
Obviously, test.someBtn does not work. Is this even possible?
Just refer to it as if it were an array:
test[someBtn]

Categories

Resources