views:

376

answers:

2

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
}
A: 

Well I've found a way... but I don't like it much...

I've added a Hidden Field control to the page, after adding the editor, and put the editor's ClientId in its value:

Dim hdn As New HiddenField
With hdn
    .ID = "HiddenField"
    .Value = itemEditor.ClientID
End With
cell.Controls.Add(hdn)

.. and then in the JavaScript, I can get the hidden field, and hence the editor name as follows:

function GetCkText()
{
    var hdn = document.getElementById("HiddenField");
    var editorName = hdn.getAttribute("value");
    var editor = CKEDITOR.instances[editorName];
    alert(editor.getData());
    return false;
}

But it's a bit inelegant, to say the least. Anyone got a better way?

ChrisA
+2  A: 

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 I hope this helps to answer your question

mcgrailm
Thanks, works fine - much less messing about. However, a question, if I may. As most of my work is .NET server-side (and I've done very little JS), I'm used to instance collections being collections of the objects themselves... whereas 'i' in your example here is the editor instance name. How would I know this - does this syntax imply a default property perhaps?
ChrisA