Use JavaScript var inside of document.formname.inputname.value [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
As the title says, I'm trying to use a var inside of a form name.
function validate_inventory_form(location){
var name=document.inventory_+[location]+_input.name.value;
...
}
What would the correct syntax be to add the location to the form name so the var name would equal the result of document.inventory_cupboard_input.name.value for example?
Thanks in advance.

You can use bracket notation to access the property, this allow you to use a custom built string as the key.
Something like document["inv" + loc + "input]

Related

Get object element from a defined string [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
how to get data from object with define string?
case:
var data = [
{name:"Sharma",country:"India"},
{name:"Udin",country:"Indonesia"},
{name:"John Carter",country:"Mars"}
];
getData(data,"country");
function getData(data,element){
console.log(data[1].element);
}
i want to get country but result is undefinded, how to fix this?
You would need to know both the index and the property
function getData(data,index,element){
console.log(data[index][element]);
}
getData(data,1,"country");
function getData(data,element){
console.log(data[1][element]);
}
That's the correct way to access the value by using a key that is a string.

Use string as object name in Javascript [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 years ago.
Is it possible to access the property of a javascript object using a string?
example: I have a javascript object "obj" that contains a property 'index'.
obj.index = 4;
Now let's say I have a string whose value is the same as a property name of my object:
var str = "index";
Can I use the value of the str variable to access the 'index' property of 'obj'?
Just use the following code:
obj[str]
You can use array Notation (squire bracket notation )
obj[str]

Using the value of a string variable as object parameter accessor [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I am attempting to dynamically access an object with a property value that is contained within a string. Example below:
var toolState = {
draw_point: false;
draw_line: false;
}
var dynamicText = "draw_point";
toolState.dynamicText = true; //here is the problem
I'm fairly new to JS. Sorry if this is a silly question.
Thanks
Use bracket notation instead of dot notation for variable names as properties.
toolState[dynamicText] = true;

Getting the value of an object's member through a variable [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
dynamically select a javascript object attribute
(1 answer)
Closed 8 years ago.
I have a javascript object (named resource) used for translating my texts.
resource
.fileNotFound >> "File not found"
.advSearch >> "Advanced search"
Usually I use this like below:
alert (resource.advSearch);
Now I need to access one of the member of this resource object through a variable.
Example:
var trans = "advSearch";
My question: how can I get the translation in my resource object for 'advSearch' contained in the trans variable?
Thanks.
You need to use the bracket notation instead of dot notation as the member operator
resource[trans]
You can use the [] notation to access properties as well.
resource[trans];

How to access a dynamic property: objectName.{variable} [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
jquery dynamic id
(4 answers)
Closed 8 years ago.
i need to access
objectName.myID
but the "myID" part is dynamically generated..
how do i do this?
i tried
this['objectName.'+ variable]
i'd hate to use eval...
ps
this happens in a function (local scope) by the way..
You can access Object properties in two ways:
o.propertyname
//or
o.["propertyname"]
When using the bracket notation you have to put the propertyname in quotes or else it will be interpreted as a variable name (which in your case is exactly what you want). So in your case where you have stored the name of the property as a string, the way to go would be:
var variable = "propertyname";
o[variable];
/* /\ variable is replace with it's string representation "propertyname" */
You can even call methods this way:
var o = {};
var functionname = 'toString';
o[functionname]();
You can mix both notations, your example would look like:
var obj = 'objectName';
var prop = 'myID';
this[obj][prop]
// or this is possible too:
this.objectName[prop]
Assuming propertyName is the name of a variable holding the name of the property, for example 'myId', then you can use.
objectName[propertyName]
More details in the MDN : Working with objects

Categories