CKEditor Character Count
First of all, this question WAS answered here: CKeditor character/word count but all the answers give broken links... therefore it's not answered.
Using CKEditor, how to I get the character count on keyup, using jquery?
Here's what should work, but does not work:
<textarea id="newKnowledge_body"></textarea>
<script>
$(function(){
// this DOES work, fyi
$("#new开发者_Python百科Knowledge_body").ckeditor();
// this DOES NOT work
$("#newKnowledge_body").keyup(function(){
var len = $("#newKnowledge_body").val().length;
alert(len);
});
});
</script>
I believe the problem lies in the "keyup" event. I don't get any response when referring to it like that... but I don't know what else to use.
Here is a quick solution with no plugins (not the best)
var len = $("#cke_contents_message").children("iframe").contents().find("body").text().length;
[UPDATE] THE BETTER WAY TO DO THAT
var editor = $('YOUR_TEXT_AREA').ckeditorGet();
editor.on("instanceReady", function(){
this.document.on("keyup", function(){
var editorContent = $(editor.getData());
var plainEditorContent = editorContent.text().trim();
console.log(plainEditorContent);
console.log("Length: "+plainEditorContent.length);
});
});
You need to register an event on the CKEditr like so:
editor.on( 'key', function( evt ){
updateCount( evt.editor.getData() );
}, editor.element.$ );
I also found this link that has a nice character count using jQuery and css. http://jsfiddle.net/VagrantRadio/2Jzpr/
Try using .change()
instead of .keyup()
精彩评论