I want to run a function when a user edits the content of a div with contenteditable attribute. What’s the equivalent of an onchange event?
I’m using jQuery so any solutions that uses jQuery is preferred. Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
2022 update
As pointed out in the comments, this doesn’t answer the question asked, which wanted the equivalent of the
changeevent rather than theinputevent. However, I’ll leave it here as is.Original answer
I’d suggest attaching listeners to key events fired by the editable element, though you need to be aware that
keydownandkeypressevents are fired before the content itself is changed. This won’t cover every possible means of changing the content: the user can also use cut, copy and paste from the Edit or context browser menus, so you may want to handle thecutcopyandpasteevents too. Also, the user can drop text or other content, so there are more events there (mouseup, for example). You may want to poll the element’s contents as a fallback.UPDATE 29 October 2014
The HTML5
inputevent is the answer in the long term. At the time of writing, it is supported forcontenteditableelements in current Mozilla (from Firefox 14) and WebKit/Blink browsers, but not IE.Demo:
Demo: http://jsfiddle.net/ch6yn/2691/