Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unselect what was selected in an input with .select()

After I use .select() to select the text in the input box when hovered over I did the following:

HTML:

<input type="text" class="hoverAble" value="hover here"/><br />

jQuery:

$(".hoverAble").mouseenter(function() {

    this.select();

}).mouseleave(function() {

    //I can't figure what to put here.
});

See here. Warning for it to function correctly (in jsfiddle) you must click once in the result frame.

The main idea is mouseleave is working as as expected also.

As you might have noticed, I can't figure out a way to un-select the text when you hover out and avoid this:

enter image description here

like image 878
Trufa Avatar asked May 02 '11 19:05

Trufa


2 Answers

use .blur();

http://jsfiddle.net/robert/adCfw/6/

like image 79
Robert Avatar answered Oct 24 '22 16:10

Robert


In this case, blur() only unfocus from the input text, and although it gives the appearance the text is not selected anymore, it is still selected. To truly unselect any text, you would do:

$(".hoverAble").mouseenter(function() {
    this.select();
}).mouseleave(function() {
    $(this).prop('selectionStart', 0).prop('selectionEnd',0).blur();
});
like image 43
Jacques Avatar answered Oct 24 '22 16:10

Jacques