开发者

Use Javascript variable in object name? [duplicate]

This question already has answers here: Accessing an object property with a dynamically-computed name (17 answers) Closed 8 years ago.

I am using CKEditor, and, when referring to the CKEditor instance, I need to use a variable. But, since calling the 开发者_StackOverflowinstance is a object, I am really not sure how to do this.

I am using:

CKEDITOR.instances.textarea123.insertHtml('<p>Whatever</p>');

The issue is, I need 123 to be a variable, because I need to change the instance based on the editor page that is loaded.

So, how can I use a variable in an object name?

For obvious reasons the following does not work, but I need to achieve what it is "pretending" to do:

var id = 354;
CKEDITOR.instances.textarea+id+.insertHtml('<p>Whatever</p>');


Try the following:

var id = 354;
CKEDITOR.instances['textarea'+id].insertHtml('<p>Whatever</p>');


You can use array notation:

CKEDITOR.instances['textarea' + id].insertHtml('<p>Whatever</p>');


var id = 354;
CKEDITOR.instances["textarea" + id].insertHtml('<p>Whatever</p>');

Since instances is an object, and objects essentially are hash tables you can access them with the array notation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜