2

I am using CKEditor, and, when referring to the CKEditor instance, I need to use a variable. But, since calling the instance 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>');
Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412

3 Answers3

5

Try the following:

var id = 354;
CKEDITOR.instances['textarea'+id].insertHtml('<p>Whatever</p>');
Stoo
  • 234
  • 1
  • 2
4

You can use array notation:

CKEDITOR.instances['textarea' + id].insertHtml('<p>Whatever</p>');
Greg
  • 316,276
  • 54
  • 369
  • 333
2
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.

Tobias Baaz
  • 1,840
  • 1
  • 12
  • 8