I want to display formulas with MathJax and edit them with CKEditor's Mathematical Formulas. However when I switch from edit mode to display mode the formulas appear blank (being initially and in the edit mode displayed properly). I've been able to reduce it to a small jsfiddle:
Steps to reproduce it:
However if you inspect the element you'll see how the initial code in the step 1 and the one after this step is the same but now it's not shown.
The HTML code:
<div id = "fullarticle" contenteditable = "false">
<p>Some text</p>
<span class = "math-tex">
\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)
</span>
<p>Some more text</p>
</div>
<button class = "edit">Edit</button>
<button class = "save">Save</button>
The javascript code:
$(".edit").click(function(){
$(".math-tex").each(function(index){
$(this).html("\\(" + $(this).find("script").html() + "\\)");
});
$("#fullarticle").attr("contenteditable", "true");
CKEDITOR.inline('fullarticle');
});
$(".save").click(function(){
var mytext = CKEDITOR.instances.fullarticle.getData();
$("#fullarticle").html(mytext).attr("contenteditable", "false");
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
for(k in CKEDITOR.instances) {
var instance = CKEDITOR.instances[k];
instance.destroy();
}
});
Edit:
After some testing, I can confirm it has to do to the fact that I'm mixing CKEditor and MathJax, since in this fiddle the issue cannot be seen.
I got it working. Apparently is some kind of timing/order issue:
The new Javascript:
$(".edit").click(function(){
$(".math-tex").each(function(index){
$(this).html("\\(" + $(this).find("script").html() + "\\)");
});
$("#fullarticle").attr("contenteditable", "true");
CKEDITOR.inline('fullarticle');
});
$(".save").click(function(){
$("#fullarticle").attr("contenteditable", "false");
for(k in CKEDITOR.instances) {
var instance = CKEDITOR.instances[k];
instance.destroy();
}
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
});
However, you cannot edit/save multiple times. Try it, the second consecutive time that you press edit it will display really bad.