Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  77] [ 1]  / answers: 1 / hits: 17482  / 12 Years ago, thu, december 27, 2012, 12:00:00

First off I'll start off by saying I am new to javascript so hopefully this isn't a complete face palm question. That being said, the following code should alert the value of the editor when the user clicks off of it.



<script type='text/javascript'>
function openEditor(){
html = Hello World;
config = {
startupFocus : true
};
editor = CKEDITOR.appendTo( 'textBox', config, html );


if (editor) {
editor.on('blur', function(event) {
var ckvalue = CKEDITOR.instances.editor.getData();
alert(ckvalue);
});
}
}
</script>
<html>
<a href='#' onclick='openEditor()'>Open Editor</a><br />
<div id='textBox'></div>
</html>


Instead google chrome console reports:



Uncaught TypeError: Cannot call method 'getData' of undefined



Now when I change



var ckvalue = CKEDITOR.instances.editor.getData();


to



var ckvalue = CKEDITOR.instances.editor1.getData();


It works. This baffles me because I never declared a editor1 instance. I was hoping someone with a little more experience could explain to me why editor1 works when editor doesnt.



Here is a working example of what Im talking about: http://jsfiddle.net/s3aDC/6/


More From » ckeditor

 Answers
21

editor is a JS variable that points to CKEDITOR.instances.editor1. See that editor === CKEDITOR.instances.editor1 // true.



Moreover, event callback is executed in editor context, so this points to editor:



editor.on('blur', function(event) {
var ckvalue = this.getData();
alert(ckvalue);
});


And you can define it when initializing the editor:



var editor = CKEDITOR.appendTo( 'textBox', {
on: {
blur: function( event ) {
console.log( this.getData() );
}
}
} );


And you should definitely avoid global variables in your code! ;)


[#81195] Wednesday, December 26, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hallie

Total Points: 503
Total Questions: 114
Total Answers: 103

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
;