Set own variable in js style object [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I want to submit my own variable in the style object. I will show you what I mean:
var example = background
element.style.example // example is equal to the variable up top
Is there any with of doing this?

Assuming you want to define the property to be set dynamically, you could use the following approach:
var example = 'background';
element.style[example] = 'black';
More on how to access object properties on MDN.

You can use bracket notation
var example = "background";
element.style[example] = "#fff";

Related

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

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]

Why doesn't JavaScript convert a string to an Object? [duplicate]

This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm attempting to run a list of strings through an object. When I do it individually it works, but when I pass it through as a string it doesn't work. How would I fix this?
// this doesn't work
var a = "IntegrationItem1";
var data = faq.a;
// but this works
var data = faq.IntegrationItem1;
What's causing the first example to not work? Is the variable data seeing it as faq."IntegrationItem1" instead of faq.IntegrationItem1?
You can access properties of the object using it's names:
var a = "IntegrationItem1";
var data = faq[a];
what you need is faq["IntegrationItem1"] => faq[a]

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;

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

get dynamic properties in javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
var tinymce_toolbar = {}
tinymce_toolbar.__default =
{
script_url: '/cms/libs/js/manual/renders/tiny_mce/tiny_mce.js',
};
tinymce_toolbar.__simple =
{
script_url: '/cms/libs/js/manual/renders/tiny_mce/tiny_mce_simple.js',
};
// Doesn't work
var t = $(this).find('input[name=toolbar]').first().val();
$('.RenderHtmlEditor').tinymce(tinymce_toolbar.t);
// works
var t = $(document).find('input[name=toolbar]').first().val();
$('.RenderHtmlEditor').tinymce(tinymce_toolbar.__default);
$('.RenderHtmlEditor').tinymce(tinymce_toolbar.__simple);
how i do it to be dynamic? Thanks
object['name'] is quite same way as object.name. simply assign a associative attribute and use it as a property.
Instead of dot notation,
tinymce_toolbar.t
Use subscript notation:
tinymce_toolbar[t]

Categories