Is there a eventListener who is just checking the changes of an innerText?
Example:
12345
I just want to check if "12345" is changing to something else like "23415".
There is no "click" event that will comes up or anything else. I don't have the chance to go through the code where the updates from the span's are coming. So I really can just only grab the event from the changes in the span element but I don't have any clue how.
Sorry for my bad English and thanks!
The innerText property of the HTMLElement interface represents the rendered text content of a node and its descendants. As a getter, it approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied it to the clipboard.
The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the checked state has been changed. Tip: This event is similar to the oninput event.
An event listener is a procedure in JavaScript that waits for an event to occur. The simple example of an event is a user clicking the mouse or pressing a key on the keyboard.
The method addEventListener() works by adding a function, or an object that implements EventListener , to the list of event listeners for the specified event type on the EventTarget on which it's called.
Check out the MutationObserver. Using it you can listen to changes of the observed element's characterData
. Example:
<span class="observable" contenteditable="true">12345</span>
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);
FIDDLE
Note that subtree
has to be true
because the text actually is a child element of the observed element.
Just in case anyone is looking for a way to do this for multiple nodes. fiddle Further reading
HTML
<span class="observable" contenteditable="true">13436</span>
<span class="observable" contenteditable="true">13235</span>
JS
var observables = document.querySelectorAll('.observable');
console.log(observables);
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(mutation);
});
});
var config = {
characterData: true,
subtree: true
};
observables.forEach(function(node) {
observer.observe(node, config);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With