Trigger the select event of an <input> element:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>//from w w w . j a v a 2 s . c om <script> $(document).ready(function(){ $("input").select(function(){ $("input").after(" Select event triggered!"); }); $("button").click(function(){ $("input").triggerHandler("select"); }); }); </script> </head> <body> <input type="text" value="Hello World"><br><br> <button>Trigger the select event for the input field</button> </body> </html>
The triggerHandler()
method triggers the specified event.
The trigger()
method also triggers the default behavior of an event.
While the triggerHandler()
method only trigger the handler.
$(selector).triggerHandler(event,param1,param2,...)
Parameter | Optional | Description |
---|---|---|
event | Required. | the event to trigger for the specified element |
param1,param2,... | Optional. | Additional parameters to pass on to the event handler. |