views:

56

answers:

4

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;
A: 

Are you using asp.net?

Adeilson
This should have belonged to comments, but I guess you didn't have enough rep.
Alex Bagnolini
+6  A: 

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 becayse it is a correct statement saying "get the function focus" (and do nothing with it).

Alex Bagnolini
+5  A: 

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.

Arun P Johny
+2  A: 

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

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

For reference, see focus documentation.

wsanville