How can I use onChange
or a similar event for all form
elements? I don't want to use onChange
for each field separately.
Definition and UsageThe 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.
The onchange attribute fires the moment when the value of the element is changed. Tip: This event is similar to the oninput event. The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus.
No; the onchange attribute is only applicable to form field elements ( input , select , textarea , etc). Thanks for your answer.
The problem is that buttons can't have a onChange property. Why? Because buttons don't have a field to change with. All the elements in HTML who can be used with a onChange property have a field.
You can use the change
event on the form
element:
var form = document.querySelector('form'); form.addEventListener('change', function() { alert('Hi!'); });
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