jQuery select()

The jQuery select() method is used to attach a function to run when a select event occurs i.e. when a text is marked or selected in a text area or a text field. This method is used for input type=”text” fields and textarea boxes.

Syntax:

To trigger the select event for selected elements.

$(selector).select()

To add a function to the select event.

$(selector).select(function)

Function:

  • It is an optional parameter.
  • The function parameter specifies the function to run when the event occurs.

Example:

<!DOCTYPE html>
<html>
<head>
<style>
p {
color: cyan;
}
div {
color: turquoise;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p>Select the text on the box</p>
<input type="text" value="Hello guys!">
<input type="text" value="myfriends.org">
<div></div>
<script>
$( ":input" ).select(function() {
$( "div" ).text( "You selected some text" ).show().fadeOut( 1000 );
});
</script>
</body>
</html>