Select all the text in the input, focusin

I want all the text to be selected when I focus the element.

I used

$('.class-focusin').focusin(function(e) { $(this).select(); }); 

But this is not the ideal behavior that I want. When I click between two characters, the input value is not highlighted, the cursor is in the middle. I need the behavior of the url string in the Chrome browser: you click and the cursor is replaced by a choice of value

Feel free to check out my problem in this fiddle: http://jsfiddle.net/XS6s2/8/

I want the text to be able to get the cursor after selecting it. The answer selected below is definitely what I wanted, thanks

+7
javascript jquery
source share
5 answers

You can have a variable to see if the text was selected or not:

http://jsfiddle.net/jonigiuro/XS6s2/21/

 var has_focus = false; $('.class-focusin').click(function(e) { if(!has_focus) { $(this).select(); has_focus = true; } }); $('.class-focusin').blur(function(e) { has_focus = false; }); 
+2
source share

I think you need to wait until the browser gives focus to the element, and then make your choice:

 $('.class-focusin').focus(function(e) { var $elem = $(this); setTimeout(function() { $elem.select(); }, 1); }); 

Ugh. Or just bind the click event:

 $('.class-focusin').click(function(e) { $(this).select(); }); 
+1
source share

Try using click instead of focusin.

 $('.class-focusin').click(function(e) { $(this).select(); }); 

Demo

Bye!

0
source share

Working demo http://jsfiddle.net/cse_tushar/XS6s2/22/

 $(document).ready(function () { $('.class-focusin').click(function (e) { $(this).select(); }); }); 
0
source share

This is a direct duplicate. Select all the text in the input, focusin , here is a good answer from it.

 $(".class-focusin").on("focus",function(e){ $(this).select(); }); $(".class-focusin").on("mouseup",function(e){ e.preventDefault(); }); 

http://jsfiddle.net/XS6s2/31/

0
source share

All Articles