JavaScript jQuery Cleditor get textarea value on keyup

I'm using Cleditor http://premiumsoftware.net/cleditor/docs/GettingStarted.html. I want to get the value on keyup and insert the text into another div. cleditor comes with change() event that i'm currently using in the jsfiddle example below, but thats not the same as keyup. I want the div to be updated as i'm typing in. I tried keyup but it doesn't work.

Here's what i have now

$("#input").cleditor().change(function(){
    var v = $('#input').val();
    $('#x').html(v);
})

Check jsfiddle http://jsfiddle.net/qm4G6/11/

Answer:1

It appears that cleditor hides the textarea and replaces it with an iframe (see line 203 of cleditor source).

So to achieve what you want, you just need to access the resulting iframe contents:

$("#input").cleditor();

$(".cleditorMain iframe").contents().find('body').bind('keyup', function(){
    var v = $(this).text(); // or .html() if desired
    $('#x').html(v);
});

Updated jsFiddle

UPDATE to address Tim's comment

This works in Chrome and Firefox (I don't have access to IE):

$("#input").cleditor();

$( $(".cleditorMain iframe")[0].contentWindow.document ).bind('keyup', function(){
    var v = $(this).text(); // or .html() if desired
    $('#x').html(v);
});

Updated jsFiddle

UPDATE 2

User ima007 was able to find a better cross-browser solution: jQuery Cleditor wysiwyg text editor: keyup() works in webkit browsers but not Firefox or IE

Answer:2

I was able to achieve this by slightly modifying the source code of the editor - in refresh method (line 801) I modified the blur event handler of iframe doc.

Previous

// Update the textarea when the iframe loses focus
    ($.browser.mozilla ? $doc : $(contentWindow)).blur(function() {
      updateTextArea(editor, true);
    });

Modified to

// Update the textarea when the iframe loses focus or keyup happens
        ($.browser.mozilla ? $doc : $(contentWindow)).bind('blur keyup', function (e) {
            updateTextArea(editor, true);

            if (options.keyup && e.type === 'keyup')
                options.keyup(editor.$area.val());
        });

and in the options that are passed at the time of initialisation, you can define

$("#element").cleditor({
keyup : function (text) {
 alert(text);
 // do something
}
});

Hope this helps anyone.

Regards

Answer:3

Browsing Hacker News and I come across http://streamjs.org/ which is an implementation of a lazy evaluated collection in Javascript. One of the examples is this: function ones() { return new ...

Browsing Hacker News and I come across http://streamjs.org/ which is an implementation of a lazy evaluated collection in Javascript. One of the examples is this: function ones() { return new ...

This is code from a book I have explaining recursion. The problem is that I don't understand the steps taken by the program: var hanoi = function(disc,src,aux,dst) { if (disc > 0) { ...

This is code from a book I have explaining recursion. The problem is that I don't understand the steps taken by the program: var hanoi = function(disc,src,aux,dst) { if (disc > 0) { ...

I'm experimenting with the HTML5 File API, and I'm needing to use a method which I don't know enough about (simply because it's hardly documented anywhere). I'm talking about the FileWriter truncate()...

I'm experimenting with the HTML5 File API, and I'm needing to use a method which I don't know enough about (simply because it's hardly documented anywhere). I'm talking about the FileWriter truncate()...

*Is there a way to call a function defined inside another function in javaSCRIPT? For example: window.onload() = function() { function my_function(){ print("Blah"); }; }; function ...

*Is there a way to call a function defined inside another function in javaSCRIPT? For example: window.onload() = function() { function my_function(){ print("Blah"); }; }; function ...

  1. call that function
  2. calling function with arguments in python
  3. calling function with self
  4. calling function is c++
  5. calling the function in python
  6. calling the function in javascript
  7. calling the function in matlab