When a divs value is changed, how Can I trigger an event?
<div class="changeable" contenteditable="true"> Click this div to edit it <div>
So when its content changes I want to create an alert and/or do other things:
$('.changeable').text().change(function() {
alert('Handler for .change() called.');
});
You can simply use focus/blur event with data() function of jQuery :
UPDATE: With Vanilla JS
Yet another version in jquery. View in jsFiddle here.
I built a jQuery plugin to do this.
You can simply call
$('.wysiwyg').wysiwygEvt();
You can also remove / add events if you wish
Another solution which is slightly modified version of previous ones but it may be more comfortable to use for some of you.
Idea is to save origin value and compare it with current value in 'blur' event. I save origin value as attribute of editable div tag (instead of creating variable for origin value). Using attribute as container for origin value is more comfortable when one has a huge number of editable divs in table (as cells). So it is easy to get origin value since you don't need to create many variables.
See my complete example code:
editable div
detecting changes in blur
What you have to do is make a conjunction of
blur
andDOMSubtreeModified
events on yourcontenteditable
elements.