1

Possible Duplicate:
Focus input field with JQuery without selecting current value text

Using the following code to focus the first field in the form:

$(document).ready(function(){
  $("form input:visible:enabled:first").focus();        
});

Why does it select the text in the field? How to focus the input without selecting the text?

Working in Ruby on Rails. Tested on Chrome.

Community
  • 1
  • 1
B Seven
  • 44,484
  • 66
  • 240
  • 385

1 Answers1

2
$(document).ready(function(){
   $("form input:visible:enabled:first").focus().val('Yeah');        
});​

If you dunno what the value is going to be, you can do:

var $firstInput = $("form input:visible:enabled:first")
$firstInput.focus().val($firstInput.val());

I'm going to try to find a better solution, though.

Belladonna
  • 3,824
  • 2
  • 24
  • 33