jQuery Cleditor get textarea value on keyup

爷,独闯天下 提交于 2019-12-03 06:18:14
dgilland

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

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

Have you tried using of CLEditor '.doc' property?

doc - The document object currently being edited in the iframe. cleditor documentation

var inputDoc = $("#input").cleditor().doc;

$(inputDoc).keyup(function(){
    var v = $('#input').val();
   $('#x').html(v);
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!