I am using codemirror in a webapp I am creating and I have several editors on the page. There are undo and redo buttons as well and they do work. However I can only set one editor to undo unless I use an if statement to determine which one is focussed and then apply the undo or redo function to its corresponding textbox. I have tried a few variations in both jQuery and JavaScript but to no avail. This is one of the code blocks I am attempting to get to work. The actual variable that holds the codemirror setup is called "codeeditor1", "#editor1" is the id of the textbox.
if ($("#editor1").is(':focus')) {
undo.addEventListener('click', function() {
codeeditor1.undo();
}, false);
redo.addEventListener('click', function() {
codeeditor1.redo();
}, false);
}
I also tried this according to the documentation of the method "cm.hasFocus()" = boolean.
if (codeeditor1.hasFocus() == true) {
undo.addEventListener('click', function() {
codeeditor1.undo();
}, false);
redo.addEventListener('click', function() {
codeeditor1.redo();
}, false);
}
I have now also tried this for the sake of logical code placement but still no luck with it. Perhaps it is a bug in the method of codemirror?
undo.addEventListener('click', function() {
if (codeeditor1.hasFocus() == true) {
codeeditor1.undo();
}
}, false);
redo.addEventListener('click', function() {
if (codeeditor1.hasFocus() == true) {
codeeditor1.redo();
}
}, false);
Try to
if( editor.hasFocus() == true )
// This editor is Codemirror object
// not jquery element.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With