jQuery select() with Examples


The select() method in jQuery is used to trigger the select event.

syntax

The syntax is as follows −

$(selector).select(func)

Above, the func parameter is used to specify the function to run when the select event is triggered. It occurs when a text is selected. The selection should be in a text area or a text field.

Let us now see an example to implement the jQuery select() method 

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("input").select(function(){
         alert("You have selected some text...");
      });
   });
</script>
<style>
div {
   border: 3px dashed orange;
   background-color: gray;
   width: 400px;
   height: 100px;
   padding: 40px;
}
</script>
</style>
</head>
<body>
<div>
<input type="text" value="Demo text">
</div>
</body>
</html>

Output

This will produce the following output −

On selecting any text in the input box, the following alert box will be visible −

Updated on: 31-Dec-2019

209 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements