Contents:
.on( "select" [, eventData ], handler )Returns: jQuery
Description: Bind an event handler to the "select" event.
-
version added: 1.7.on( "select" [, eventData ], handler )
This page describes the select
event. For the deprecated .select()
method, see .select()
.
The select
event is sent to an element when the user makes a text selection inside it. This event is limited to <input type="text">
fields and <textarea>
boxes.
For example, consider the HTML:
1
2
3
4
5
6
|
|
The event handler can be bound to the text input:
1
2
3
|
|
Now when any portion of the text is selected, the alert is displayed. Merely setting the location of the insertion point will not trigger the event. To trigger the event manually, use .trigger( "select" )
:
1
2
3
|
|
After this code executes, clicks on the Trigger button will also alert the message:
Handler for `select` called.
In addition, the default select
action on the field will be fired, so the entire text field will be selected.
The method for retrieving the current selected text differs from one browser to another. A number of jQuery plug-ins offer cross-platform solutions.
Examples:
To do something when text in input boxes is selected:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
|
Demo:
To trigger the select event on all input elements, try:
1
|
|
.trigger( "select" )Returns: jQuery
Description: Trigger the "select" event on an element.
-
version added: 1.0.trigger( "select" )
-
"select"Type: stringThe string
"select"
.
-
See the description for .on( "select", ... )
.