Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting an "unselect" event in a text area

jQuery has the .select() method, which lets you execute a function when text is selected in a text area.

Unfortunately, there is no corresponding .deselect() method for executing a function when the user removes a selection.

How can I detect when the user has unselected the text (for example, by typing, clicking, or defocusing)?

like image 496
Amanda S Avatar asked Oct 08 '22 09:10

Amanda S


1 Answers

$("textarea").select(function() {
    //do something here
});

$("textarea").on("blur focus keydown mousedown", function() {
    //do something else here
});​

.on is used in jquery 1.7 and later. Prior to that you can use .delegate or .live

http://jsfiddle.net/s29Vb/4/

It was simplified for the example. You can be more specific with the selector rather than $("textarea").

Another example if you want to do something on blur focus keydown mousedown only if text has been selected in the first place.

http://jsfiddle.net/s29Vb/5/

like image 100
jk. Avatar answered Oct 12 '22 10:10

jk.