简体   繁体   中英

CodeMirror using jQuery .keyup on editor textarea

I want to getValue of Codemirror editor on keyup but it did not work. Here's the fiddle

var mixedMode = {
        name: "htmlmixed",
        scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
                       mode: null},
                      {matches: /(text|application)\/(x-)?vb(a|script)/i,
                       mode: "vbscript"}]
      };
      var editor = CodeMirror.fromTextArea(document.getElementById("HTML"), {mode: mixedMode,lineNumbers: true  });

$(document).ready(function(){
  $("#HTML").keyup(function(){
    html = editor.getValue();
    alert(html);
    });
}); 

CodeMirror hides the textarea element, for listening to the events of the editor instance you can use the on method:

$(document).ready(function () {
    editor.on('change', function () {
        html = editor.getValue();
        alert(html);
    });
});

You can find the list of the supported events in the CodeMirror's manual.

http://codemirror.net/doc/manual.html#events

editor.on("keyup", function(cm, event) {
    //only show hits for alpha characters
    if(!editor.state.completionActive && (event.keyCode > 65 && event.keyCode < 92)) {
        if(timeout) clearTimeout(timeout);
        var timeout = setTimeout(function() {
            CodeMirror.showHint(cm, CodeMirror.hint.clike, {completeSingle: false});
        }, 150);
    }
});

Replace 'CodeMirror.hint.clilke' with whichever hint mode you are using :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM