Use Javascript variable in object name?

I am using CKEditor, and referring to the CKEditor instance, I need to use a variable. But, since calling an instance is an object, I'm really not sure how to do this.

I use:

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

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

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

For obvious reasons, the following does not work, but I need to get it to "pretend":

 var id = 354; CKEDITOR.instances.textarea+id+.insertHtml('<p>Whatever</p>'); 
+2
javascript object ckeditor
Oct. 14 '09 at 15:57
source share
3 answers

Try the following:

 var id = 354; CKEDITOR.instances['textarea'+id].insertHtml('<p>Whatever</p>'); 
+5
Oct 14 '09 at 16:01
source share

You can use array notation:

 CKEDITOR.instances['textarea' + id].insertHtml('<p>Whatever</p>'); 
+4
Oct 14 '09 at 16:00
source share
 var id = 354; CKEDITOR.instances["textarea" + id].insertHtml('<p>Whatever</p>'); 

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

+2
Oct. 14 '09 at 15:59
source share



All Articles