jQuery trigger()

Introduction

Trigger the select event of an <input> element:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>//from   w ww  .ja  v  a 2 s .  c o  m
<script>
$(document).ready(function(){
  $("input").select(function(){
    $("input").after(" Text marked!");
  });
  $("button").click(function(){
    $("input").trigger("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 trigger() method triggers the specified event.

The trigger() method triggers the default behavior of an event for the selected elements.

$(selector).trigger(event,event_Obj,param1,param2,...)
Parameter
Optional
Description
event

Required.

event to trigger for the specified element.
We can use a custom event, or the standard events
param1,param2,...
Optional.
Additional parameters to pass on to the event handler.



PreviousNext

Related