I'm writing a user script which needs to respond to an <input>
element's value changing, no matter how the change was made.
For user interactions, this is easy — just add a listener for the input
event. However, I haven't yet found a way to respond to the input's value being set directly by changing its .value
property.
I've tried adding listeners for the change
and input
events (neither are triggered) and adding a MutationObserver
for both the input's attributes and the input's parent's child list (again, neither are triggered). There also doesn't appear to be a property descriptor I can modify.
How can I trigger code to run when an <input>
element's .value
property is set?
<!DOCTYPE html>
<html>
<head>
<title>Detect value change</title>
<script type="module">
const range = document.querySelector('input');
range.addEventListener('change', (event) => console.log('Change:', event));
range.addEventListener('input', (event) => console.log('Input:', event));
const observer = new MutationObserver((mutations) => console.log('Mutation:', mutations));
observer.observe(range, { attributes: true, attributeFilter: ['value'] });
observer.observe(range.parentNode, { childList: true });
console.log('Property descriptors:', Object.getOwnPropertyDescriptors(range));
// Somewhere else, in code I don't control:
document.querySelector('input').value = 7;
</script>
</head>
<body>
<input type="range" min="0" max="10" />
</body>
</html>
Console output:
Property descriptors: {}