0

I need to prevent users from hitting the enter key if the cursor is in certain positions:

        this.contentArea.addEventListener('keypress', e => {
            if (e.keyCode == 13) {
                if (insideForbiddenElement)
                    e.preventDefault();
            }
        });

Is there a way to consistently get this? It only needs to work in the latest firefox / chrome.

stackers
  • 2,701
  • 4
  • 34
  • 66
  • It is only supposed to prevent if the cursor is on the `insideForbiddenElement` element? – Emiel Zuurbier Sep 01 '19 at 22:21
  • it would be inside an anchor element for example, I don't want to add line breaks inside links – stackers Sep 01 '19 at 22:23
  • Could you add the element(s) it is supposed to prevent on in your code? That seems like a crucial part. – Emiel Zuurbier Sep 01 '19 at 22:25
  • Possible duplicate of [Get caret (cursor) position in contentEditable area containing HTML content](https://stackoverflow.com/questions/4767848/get-caret-cursor-position-in-contenteditable-area-containing-html-content) – Heretic Monkey Sep 02 '19 at 00:16

0 Answers0