There is no inbuilt solution to this problem, this is a problem with your design and coding pattern.
You can use publisher/subscriber pattern. For this you can use jQuery custom events or your own event mechanism.
First,
function changeHtml(selector, html) {
var elem = $(selector);
jQuery.event.trigger('htmlchanging', { elements: elem, content: { current: elem.html(), pending: html} });
elem.html(html);
jQuery.event.trigger('htmlchanged', { elements: elem, content: html });
}
Now you can subscribe divhtmlchanging/divhtmlchanged events as follow,
$(document).bind('htmlchanging', function (e, data) {
//your before changing html, logic goes here
});
$(document).bind('htmlchanged', function (e, data) {
//your after changed html, logic goes here
});
Now, you have to change your div content changes through this changeHtml()
function. So, you can monitor or can do necessary changes accordingly because bind callback data argument containing the information.
You have to change your div's html like this;
changeHtml('#mydiv', '<p>test content</p>');
And also, you can use this for any html element(s) except input element. Anyway you can modify this to use with any element(s).
keypress
event to a contenteditable<div>
element. I'm not sure the solutions there apply to this. They definitely wouldn't pick up any programmatic changes to the content of an element. – Redingote