How insert variable in JS [duplicate] - javascript

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;
}

Related

Get variable by variable [duplicate]

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"

Calling javascript function using dot operator [duplicate]

This question already has answers here:
How to observe value changes in JS variables
(3 answers)
Closed 5 years ago.
function getPercCalculated(x){
return (x*9.3)/100;
}
var x = 10;
var perx = getPercCalculated(x);
Instead of this I would like to call the getPercCalculated using dot operator like
var perx = x.getPercCalculated()
Can someone help me..!!
Number.prototype.getPercCalculated= function(){
return (this*9.3)/100;
};
This will attach the getPercCalculated to every number in your code tough

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);

Use a variable to a variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I want to pass a variable into a variable
This is what I want
uk.objects.ARG
I have
var d= ARG
how can I get uk.objects.ARG?
I tried
uk.objects.d
, javascript stores it as the string, not a variable
What should I do in order to make it as a variable ?
I think what you are looking for is perhaps:
var d = "ARG";
uk.objects[d];
You can access JavaScript objects' properties also with the bracket notation:
object[property] = value;
The same works for reading. So in your case you could do the following:
var value = uk.objects[d]

Categories

Resources