6

Prototype's activate function

Gives focus to a form control and selects its contents if it is a text input

according to the Prototype website. i.e.

$('my_element_id').activate();

What is the equivalent function in jQuery?

Rob W
  • 341,306
  • 83
  • 791
  • 678
Mike Webb
  • 8,855
  • 18
  • 78
  • 111

4 Answers4

5
$('#my_element_id').focus();

which is a shortcut for

$('#my_element_id').trigger('focus');

http://api.jquery.com/focus/

jAndy
  • 231,737
  • 57
  • 305
  • 359
4

Prototype's activate() function focuses on and selects the entire contents of the elements of the form.

In JQuery, this behavior can be replicated with three functions:

// Focus
$('my_element_id').focus();

// Focus and select the content.
$('my_element_id').focus().select();

// Focus and select the content without problems in Google chrome
$('my_element_id').focus().select().mouseup(function(event){
  event.preventDefault();
});
edwines
  • 367
  • 2
  • 10
2
$('my_element_id').focus();
Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
1

jQuerys focus() method does not select the text in the input field. Instead, add select():

$('my_element_id').focus().select();
acme
  • 14,654
  • 7
  • 75
  • 109