Javascript array value not printing [duplicate] - javascript

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Access object properties within object [duplicate]
(2 answers)
Closed 6 years ago.
How to do ElementName get value from ElementId?
var Elements = {
ElementId: '#myelement',
ElementName: Elements.ElementId
};
alert(Elements.ElementName);

You haven't created the object yet, so it's not going to be defined. You can define that property on the object once it's created like this:
var Elements = {
ElementId: '#myelement'
};
Elements['ElementName'] = Elements.ElementId;
alert(Elements.ElementName);
However I think there's a better way; what specifically are you using the name and id properties for?

Related

How can I add an element to a json object in JavaScript? [duplicate]

This question already has answers here:
How to concatenate properties from multiple JavaScript objects
(14 answers)
Closed 6 months ago.
var obj={"student":[{"id":1,"name":"mark"}]}
For example I have this object I want to add another element to it such as this:
var teacher= "teacher":[{"id":1,"name":"Stacy"}]
to make this new object:
var objnew= {"student":[{"id":1,"name":"mark"}], "teacher":[{"id":1,"name":"Stacy"}] }
You can use this code.
var objnew = Object.assign({}, obj, teacher);

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]

How to get the prototype property in Javascript? [duplicate]

This question already has answers here:
Why is JavaScript prototype property undefined on new objects?
(5 answers)
How does JavaScript .prototype work?
(26 answers)
Closed 7 years ago.
I define an object using function
AAA = function ( x) {
this.x = x || 0;
};
Then I created on object using
var a = new AAA();
If I am using a.prototype, I get "undefined". I can only use AAA.prototype to access the prototype property, I would like to know why?

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;

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