Search code examples
jquerycodemirror

CodeMirror only being submitted after second click


When I click send on my ajax form, all fields are submitted via POST except for CodeMirror. Only after a second click, all future forms are being submitted correctly.

What am I doing wrong?

Thanks!

$(document).ready(function() {
    var options = { 
        target:     '#code',
        beforeSubmit:  function() {
            $('input#submit').attr('value','loading...');
        },
        success:    function() { 
            $('input#submit').attr('value','Submit');
        } 
    };  
    $('#form').submit(function() { 
        $(this).ajaxSubmit(options);
        return false; 
    });


    var editor = CodeMirror.fromTextArea(document.getElementById('codeenter'), {
        height: "150px",
        parserfile: "parsecss.js",
        stylesheet: "csscolors.css",
        path: "js/"
    });
});    

Solution

  • var editor = CodeMirror.fromTextArea(document.getElementById('codeenter'), {
        height: "150px",
        parserfile: "parsecss.js",
        stylesheet: "csscolors.css",
        path: "js/"
    });
    

    Try to add an onBlur Event:

    var editor = CodeMirror.fromTextArea(document.getElementById('codeenter'), {
        height: "150px",
        parserfile: "parsecss.js",
        stylesheet: "csscolors.css",
        path: "js/"
        onBlur: function () { 
             editor.save();             
        }
    });
    

    It will save the content of the editor back to the textarea if the editor loses focus. Solved the problem in my case.