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]
Related
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]
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";
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;
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
Please explain, why this code it's not allowed in javascript and how to make it.
var p = "inputText";
regError.p
This will give me undefined but
regError.inputText
will give me a correct result.
You can do it by using bracket notation:
regError[p]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors
If you have an object like this
var regError = {
inputText : 'something'
}
and you want to access it with a variable, you'll have to use bracket notation
var p = "inputText";
var result = regError[p]; // returns "something"
Use with bracket notation:
regError[p]
You can check the difference between them here and there
This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 3 years ago.
Needing to do:
var some = {
`${foo1}_${foo2}`: bar
}
but this gives a syntax error though I must do it somehow. How?
you can suppose object as hashmap and access properties via []
var foo1 = 'a';
var foo2 = 'b';
var some = {};
some[foo1+'_'+foo2] = 'test';
console.log(some.a_b);