Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply CodeMirror to pre-existing div

I have a div called content_editable. I am making a code editing application. So there are certain css dimensions it needs to follow. But I want to be able to add the code mirror syntax highlighting script to it. But when I do it the way the documentation says to do it, nothing happens. At all. My div to be edited is called #editable_content. How do I add the CodeMirror capabilities to it?

EDIT

link to the site documentation CodeMirror Documentation Also, a link to a JSFiddle or a JSBin would be greatly appreciated for example reasons

like image 936
comu Avatar asked Nov 10 '11 03:11

comu


People also ask

Does GitHub use CodeMirror?

About. CodeMirror is open source under a permissive license (MIT). It is being developed on GitHub. Contributions are welcome.


1 Answers

Something like this (untested) should do the trick

var ce = document.getElementById("content_editable");
// Probably needs more kludges to be portable... you get the idea
var text = ce.textContent || ce.innerText;
var editor = CodeMirror(function(node){ce.parentNode.replaceChild(node, ce);}, {
  value: text
  /* , your options */
});
like image 97
Marijn Avatar answered Sep 28 '22 00:09

Marijn