Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically determine name of CKEditor instance

I've added a CKEditor instance programmatically to my page in the code-behind of my ASP.NET page:

VB.NET:

itemEditor = New CkEditor
cell.Controls.Add(itemEditor)

... which works fine. I can get the HTML on the postback and do stuff with it.

However, I also want to do some client-side stuff with it, specifically take a selected item out of another control, and insert it into the text by handling the onchange event.

So, how can I get the name of the editor instance in the JavaScript, so that I can do stuff like:

function GetCkText()
{
    var htmlFromEditor = CKEDITOR.instances['editorName'].getData();
    // do stuff with htmlFromEditor
}
like image 449
ChrisA Avatar asked Mar 30 '10 11:03

ChrisA


People also ask

How do I find my instance name in Ckeditor?

oEditor = CKEDITOR. currentInstance; this will give you the current instance.

How do I know if Ckeditor is loaded?

// At the top of the script CKEDitor_loaded = false; // then later CKEDITOR. on('loaded', function(){ CKEditor_loaded = true; });

How do you call Ckeditor in HTML?

To start, create a simple HTML page with a <textarea> element in it. You will then need to do two things: Include the <script> element loading CKEditor 4 in your page. Use the CKEDITOR.


3 Answers

Assuming you only have one editor instance:

for ( var i in CKEDITOR.instances ){
   var currentInstance = i;
   break;
}
var oEditor   = CKEDITOR.instances[currentInstance];

Here is what the JavaScript API says about instances.

Here is another way of defining the CKEditor. Here 'fck' is the input fields id:

CKEDITOR.replace( 'fck', {
    customConfig : prefix + 'js/ckeditor/config.js',
    height: 600,
    width: 950
});

editor = CKEDITOR.instances.fck;

Notice how I am then able to reference the instance using .fck.

like image 190
mcgrailm Avatar answered Sep 18 '22 21:09

mcgrailm


If you only have a single instance and you do not know the name of it.

CKEDITOR.instances[Object.keys(CKEDITOR.instances)[0]].getData()
like image 30
denov Avatar answered Sep 17 '22 21:09

denov


The following code:

var allInstances=CKEDITOR.instances;
for ( var i in allInstances ){
    alert(allInstances[i].name);
}

works fine for me.

like image 25
ppp Avatar answered Sep 19 '22 21:09

ppp