Click the button to trigger the change event (even if the element has not been changed).
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>/*ww w. j ava2 s . c om*/ <script> $(document).ready(function(){ $("button").click(function(){ $("input").change(); }); }); </script> </head> <body> <button>test</button> <p>Enter your name: <input value="Donald" onchange="alert(this.value)" type="text"></p> </body> </html>
The change event occurs when the value of an element has been changed.
It only happens on <input>, <textarea> and <select> elements.
The change()
method triggers the change event.
The change()
method can also run a function when a change event occurs.
For <select>, the change event occurs when an option is selected.
For text fields and text areas, the change event occurs when the field loses focus after the content has been changed.
Trigger the change event for the selected elements:
$(selector).change()
Attach a function to the change event:
$(selector).change(function)
Parameter | Optional | Description |
---|---|---|
function | Optional. | function to run when the change event occurs for the selected elements |