user1569339
user1569339

Reputation: 683

preventDefault not working with tinyMCE keydown event

When binding on the 'keydown' event on a tinyMCE editor instance, calling preventDefault() on the event does not prevent the default behavior in the editor. For example, when capturing the ENTER key being pressed with the following code:

tinymce.init({
    selector: 'textarea',
    setup: function (editor) {
        $(editor).on('keydown', function (event) {
            if (event.which == 13) {
                alert('enter pressed');
                event.preventDefault();
            }
        });
    }
});

TinyMCE still inserts a line break. How can I override this behavior?

Upvotes: 3

Views: 3042

Answers (1)

Chankey Pathak
Chankey Pathak

Reputation: 21666

Change

 if (event.which == 13) {
      alert('enter pressed');
      event.preventDefault();
 }

to

 if (event.which == 13) {
      alert('enter pressed');
      event.preventDefault();
      event.stopPropagation();
      return false;
}

Upvotes: 5

Related Questions