views:

24

answers:

1

Hi!

I'm using JQuery to load a file into a textarea and CodeMirror to colorize it, but it seems that they don't play along well: I can get the file to load or the textarea (with existing text) to get colorized, using one or the other, but when I first load it and then colorize it, I end up with a blank textarea. Here is the relevant code of my page:

$("#corpo").load("jquery-latest.min.js"); /* random js file */

var editor = CodeMirror.fromTextArea("corpo", {
 parserfile: ["tokenizejavascript.js", "parsejavascript.js"],
 path: "/codemirror/js/",
 stylesheet: "/codemirror/css/jscolors.css"
});

Thanks!

+1  A: 

Probably you have to put the colorization into a callback:

$("#corpo").load("jquery-latest.min.js", function() {
  var editor = CodeMirror.fromTextArea("corpo", {
   parserfile: ["tokenizejavascript.js", "parsejavascript.js"],
   path: "/codemirror/js/",
   stylesheet: "/codemirror/css/jscolors.css"
  });
});

... otherwise CodeMirror starts working before the content is fetched (an ajax request in jQuery performs asynchronously).

pex