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
  • 42,103
  • 65
  • 226
  • 370

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.

Bella
  • 3,754
  • 2
  • 23
  • 33