Limiting length of text and including character count
How can I limit the length of text, including xhtml markup, that a user can enter? I'd also love to have a character count, again including xhtml markup.
var charCount= document.getElementById("charCount");
/* attach event to the specific editor (should be different counts for each editor, although this code isn't perfect in that regard [you would need a different span displaying the count for each editor instance, for example]) */
CKEDITOR.on("keydown", function(event)
{
var curVal = charCount.value; //get current value
curVal += 1; //add one (this event should fire on each keydown)
charCount.value = curVal; //update the span to show the current number of characters
});
Edit
var charCount = document.getElementById("charCount");
CKEDITOR.instances.editor1.on("key", function (event)
{
charCount.innerHTML = CKEDITOR.instances.editor1.getData().length;
});
Yavin thanks for the Post but after attaching a listener to the "key" event (CKEDITOR.instances.editor1.on("key", function (event)) I find its buggy. The event does not fire properly.
CKEDITOR.instances.txtEditor.on("key", function() {
var txt= CKEDITOR.instances.txtEditor.getData();
alert(txt);
})
In the above example the text in the document is not properly shown. Is there any other solution or am I doing anything wrong?
When you use the getData function, you are getting everything including tags and spacing. The .length property of a javascript string yields the character count including empty spaces. So there is really nothing wrong because it does not count newlines or anything like that, you should only be dealing with the code tags etc.
Re: Limiting length of text and including character count
var charCount= document.getElementById("charCount"); /* attach event to the specific editor (should be different counts for each editor, although this code isn't perfect in that regard [you would need a different span displaying the count for each editor instance, for example]) */ CKEDITOR.on("keydown", function(event) { var curVal = charCount.value; //get current value curVal += 1; //add one (this event should fire on each keydown) charCount.value = curVal; //update the span to show the current number of characters });Edit
var charCount = document.getElementById("charCount"); CKEDITOR.instances.editor1.on("key", function (event) { charCount.innerHTML = CKEDITOR.instances.editor1.getData().length; });Re: Limiting length of text and including character count
CKEDITOR.instances.txtEditor.on("key", function() { var txt= CKEDITOR.instances.txtEditor.getData(); alert(txt); })In the above example the text in the document is not properly shown. Is there any other solution or am I doing anything wrong?
Re: Limiting length of text and including character count