Check out the MutationObserver. Using it you can listen to changes of the observed element's characterData
. Example:
HTML
<span class="observable" contenteditable="true">12345</span>
JS
var observables = document.querySelector('.observable');
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(mutation);
});
});
var config = {characterData: true, subtree: true};
observer.observe(observables, config);
Note that subtree
has to be true
because the text actually is a child element of the observed element.