Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
26
rated 0 times [  29] [ 3]  / answers: 1 / hits: 23688  / 13 Years ago, tue, october 18, 2011, 12:00:00

I'm trying to figure out if there is any way to listen to events like focus or change of an HTML element with contenteditable attribute.



I have this html markup:



<p id=test contenteditable >Hello World</p>


I've tried these without any success(JSBin):



var test = document.querySelector('#test');
test.addEventListener('change', function(){
alert('content edited');
}, false);
test.addEventListener('DOMCharacterDataModified', function(){
alert('content edited');
}, false);
test.addEventListener('focus', function(){
alert('content edited');
}, false);


I don't want to listen to keyboard or mouse events. I didn't find any clear documentation in W3C and MDN about contenteditable.



Is it possible to listen to change and focus or other events on a content editable HTML element?


More From » html

 Answers
19

Not really. There is no change event for contenteditable elements, and there's no HTML5 input event either, although I think that will eventually appear. It's a pain.






UPDATE 23 June 2012



Recent WebKit supports the HTML5 input event on contenteditable elements, as does Firefox 14.






focus, however, does work, as does DOMCharacterDataModified in most browsers (though notably not IE < 9). See http://jsfiddle.net/UuYQH/112/



By the way, contenteditable is not a Boolean attribute: it requires a value, which should be one of true, false, inherit and the empty string (which is equivalent to true).


[#89564] Sunday, October 16, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anjelicadixied

Total Points: 742
Total Questions: 94
Total Answers: 97

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
;