Event for changes to textarea

I have a JavaScript library that updates an HTML element hidden <textarea> with some data based on some things.

In my (JavaScript) application, I would like to know when these updates happen without having to go through the existing library code.

I was hoping there was an event for this, but apparently not.

How do I listen to text box changes?

Changes can be as simple as document.getElementById("myTextarea").value = "hello";

EDIT I will only use FireFox, as this code will only be part of the test suite that I am creating.

+5
source share
6 answers

Javascript, , , - change .

, JQuery: Events/change document.getElementById('myTextarea').onchange().

, changeFormElement(id, value).

, , , . , , , .

+3

, , , , , IE, , , . onpropertychange.

textArea.onpropertychange = function ()
{
    if (event.propertyName == "innerText")
        alert('innerText has changed.');
}

, , , , . - , , . , , , , .

+2

FireFox value:

HTMLTextAreaElement.prototype.__defineSetter__("value", function(t) {
    alert('someone is setting the value of a textarea');
    return this.textContent = t;
});
+2

DOM , node node.value; API JavaScript , .

EDITED: Firefox? , , , . . , "" , . , , , . . .

var textnode = document.createElement('textarea');
textnode.watch('value', function(attr, oldv, newv){
    alert(newv);
    return newv; // you have to return correct value back.
});

//and now trigger the change to see that this works.
textnode.value = 'Bla';

, ?:)

+1

, , :

var oldVal, el;
function setTimer(){
    setTimeout(function(){
        if(el.value != oldVal){
            console.log('something happened');
            oldVal = el.value;
        }
        setTimer();
    }, 5000);
};

.

+1

: textarea , "innerHTML",

document.getElementById("myTextarea").innerHTML = "hello";
-3

All Articles