2

What is the way to control input user (forbidden words, in instance) in ContentEditable field (Html 5) with jQuery?

<div contentEditable="true" id="title" >
...
$('#title').focus(function()
...

doesn't work.

Thanks for help.

Vincent

Vincent
  • 325
  • 5
  • 15
  • This could help : http://stackoverflow.com/questions/1391278/contenteditable-change-events Or this : http://stackoverflow.com/questions/745644/event-capturing-vs-bubbling-with-a-contenteditable-div – Tim Mar 28 '11 at 22:53

1 Answers1

4

If you want to control the user's input, you should be handling the keydown event, or something along those lines. In that event handler, if the user enters a forbidden key, you can do e.preventDefault() so that the key is not entered in the element. For eg.

$('div[contenteditable=true]').keydown(function (e) {
    e.preventDefault();
});
sharat87
  • 7,330
  • 12
  • 55
  • 80
  • @Tim. + 1. Thanks. I think it's too complex for me! :) – Vincent Mar 29 '11 at 08:59
  • `contenteditable=""` or `contenteditable="true"` Indicates that the element is editable. [Source](http://html5doctor.com/the-contenteditable-attribute/) So you would also have to check for `contenteditable=""` – Alvaro Mar 18 '16 at 14:52