stealthcopter
stealthcopter

Reputation: 14206

JQuery focus an element

In javascript the following works to give focus to the edit_2 input box:

document.getElementById("edit_2").focus();

However using Jquery this does not:

$("#edit_2").focus;

Upvotes: 25

Views: 67853

Answers (3)

Alex Bagnolini
Alex Bagnolini

Reputation: 22422

You are calling a method, so:

$("#edit_2").focus;

should be

$("#edit_2").focus();

EDIT: If you are wondering why the first line was not counted as a syntax error, it's because it is a correct statement saying "get the function focus" (and do nothing with it).

Upvotes: 79

Arun P Johny
Arun P Johny

Reputation: 388436

Your statement

$("#edit_2").focus

is not calling the function 'focus', in order to call the function you have to use the syntax 'focus()'

try

j$("#some_id").focus()

It is working fine.

EDIT Your statement '$("#edit_2").focus' is not throwing an error because it just returns a reference to the function 'focus', but it does not call the function.

Upvotes: 15

wsanville
wsanville

Reputation: 37516

focus is a function and must be called like one, change your code to look like:

$("#edit_2").focus();

For reference, see focus documentation.

Upvotes: 10

Related Questions