how to detect input value change by javascript observe 1

how to detect input value change by javascript observe

function changeValue (event, target) {
    document.querySelector("#" + target).value = new Date().getTime();
}
 
function changeContentValue () {
    document.querySelector("#content").value = new Date().getTime();
}
 
Object.defineProperty(document.querySelector("#content"), "value", {
    set:  function (t) {
        alert('#changed content value');
        var caller = arguments.callee
            ? (arguments.callee.caller ? arguments.callee.caller : arguments.callee)
            : ''
 
        console.log('this =>', this);
        console.log('event => ', event || window.event);
        console.log('caller => ', caller);
        return this.textContent = t;
    }
});

Here is what the above code is Doing:
1. It’s defining a function called changeValue that takes two arguments: event and target.
2. It’s defining a function called changeContentValue that doesn’t take any arguments.
3. It’s defining a property called value on the element with the id of content.
4. It’s defining a setter function for the value property that takes one argument: t.
5. It’s defining a variable called caller that is either the caller of the setter function or the setter function itself.
6. It’s logging the value of this, the event, and the caller.
7. It’s returning the textContent of the element with the id of content.

Now, let’s see what happens when we run the following code:

document.querySelector(“#content”).value = new Date().getTime();

Here’s what the above code is doing:
1. It’s setting the value of the element with the id of content to the current time.
2. It’s calling the setter function for the value property of the element with the id of content.

Here’s what the setter function does:
1. It’s logging the value of this, the event, and the caller.
2. It’s returning the textContent of the element with the id of content.

Here’s what the above code is logging:
1. this => [object HTMLDivElement]
2. event => undefined
3. caller => function () {
alert(‘#changed content value’);
var caller = arguments.callee
? (arguments.callee.caller ? arguments.callee.caller : arguments.callee)
: ”

console.log(‘this =>’, this);
console.log(‘event => ‘, event || window.event);
console.log(‘caller => ‘, caller);
return this.textContent = t;
}

Here’s what the above code is returning:
1. The textContent of the element with the id of content.

Now, let’s see what happens when we run the following code:

document.querySelector(“#content”).value = new Date().getTime();

Here’s what the above code is doing:
1. It’s setting the value of the element with the id of content to the current time.
2. It’s calling the setter function for the value property of the element with the id of content.

Here’s what the setter function does:
1. It’s logging the value of this, the event, and the caller.
2. It’s returning the textContent of the element with the id of content.

Here’s what the above code is logging:
1. this => [object HTMLDivElement]
2. event => undefined
3. caller => function () {
alert(‘#changed content value’);
var caller = arguments.callee
? (arguments.callee.caller ? arguments.callee.caller : arguments.callee)
: ”

console.log(‘this =>’, this);
console.log(‘event => ‘, event || window.event);
console.log(‘caller => ‘, caller);
return this.textContent = t;
}

Here’s what the above code is returning:
1. The textContent of the element with the id of content.

Now, let’s see what happens when we run the following code:

document.querySelector(“#content”).value = new Date().getTime();

Here’s what the above code is doing:
1. It’s setting the value

Similar Posts