jQuery select() Method

Last Updated:

jQuery select() Method

The jQuery select() method is used for the following 2 purpose

  1. To run a callback when a specified element is selected.
  2. To trigger the select event of a specified element.

Run a Callback

In the following example, we will run a callback function when an input text is selected.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery select() Method</h1> <input type="text" value="select me"> <script> $(document).ready(function(){ $("input").select(function(){ alert("Input text selected"); }); }); </script> </body> </html>

Syntax

$(selector).select(callback);

Parameter Values

ValueTypeExplanation
callback Optional Specifies a function to be called once the element's content is selected.

Browser Support

Devicedesktopmobile
Browsergoogle chromesafarifirefoxinternet Exploreredgeoperagoogle chromesafarifirefoxedgeoperaandroid webviewsamsung internet
select()YesYesYesYesYesYesYesYesYesYesYesYesYes

Trigger a select Event

In the following example, we will trigger a select event when a button is clicked.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery select() Method</h1> <input type="text" value="select me"> <button>Trigger select Event</button> <script> $(document).ready(function(){ $("input").select(function(){ alert("Input text selected"); }); $("button").click(function(){ $("input").select(); }); }); </script> </body> </html>

Share this Page

Meet the Author