Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Clean up JS
Source Link
rexmac
  • 1.6k
  • 11
  • 14

I was able to get this working with the following workaround:

$('textarea').focus(function() { var $this = $(this); $this.select().mouseup(function() { $this.unbindoff('mouseup'); return false; }); }).keyup(function(e) { if(e.which === 9) { $(this).select(); } }); 

Demo: http://jsfiddle.net/KfFPM/2/http://jsfiddle.net/KfFPM/3/

I tested the above in Chrome 21, Safari 6, Firefox 14, Opera 12, and IE 9. I'll test more versions later; I'm happy for now. Works when tab'ing forward, and shift+tab'ing backwards.

Binding to keydown did not work.

I still think this workaround should be unnecessary. My best guess is that webkit browsers detect the tab as a keypress inside the textarea and therefore unselect the text, the same as any browser would if you had some text selected in a textarea and then started typing.

I was able to get this working with the following workaround:

$('textarea').focus(function() { $this = $(this); $this.select().mouseup(function() { $this.unbind('mouseup'); return false; }); }).keyup(function(e) { if(e.which === 9) { $(this).select(); } }); 

Demo: http://jsfiddle.net/KfFPM/2/

I tested the above in Chrome 21, Safari 6, Firefox 14, Opera 12, and IE 9. I'll test more versions later; I'm happy for now.

Binding to keydown did not work.

I still think this workaround should be unnecessary. My best guess is that webkit browsers detect the tab as a keypress inside the textarea and therefore unselect the text, the same as any browser would if you had some text selected in a textarea and then started typing.

I was able to get this working with the following workaround:

$('textarea').focus(function() { var $this = $(this); $this.select().mouseup(function() { $this.off('mouseup'); return false; }); }).keyup(function(e) { if(e.which === 9) { this.select(); } }); 

Demo: http://jsfiddle.net/KfFPM/3/

I tested the above in Chrome 21, Safari 6, Firefox 14, Opera 12, and IE 9. I'll test more versions later; I'm happy for now. Works when tab'ing forward, and shift+tab'ing backwards.

Binding to keydown did not work.

I still think this workaround should be unnecessary. My best guess is that webkit browsers detect the tab as a keypress inside the textarea and therefore unselect the text, the same as any browser would if you had some text selected in a textarea and then started typing.

Source Link
rexmac
  • 1.6k
  • 11
  • 14

I was able to get this working with the following workaround:

$('textarea').focus(function() { $this = $(this); $this.select().mouseup(function() { $this.unbind('mouseup'); return false; }); }).keyup(function(e) { if(e.which === 9) { $(this).select(); } }); 

Demo: http://jsfiddle.net/KfFPM/2/

I tested the above in Chrome 21, Safari 6, Firefox 14, Opera 12, and IE 9. I'll test more versions later; I'm happy for now.

Binding to keydown did not work.

I still think this workaround should be unnecessary. My best guess is that webkit browsers detect the tab as a keypress inside the textarea and therefore unselect the text, the same as any browser would if you had some text selected in a textarea and then started typing.