views:

272

answers:

1

Anyone know why .focus() makes the cursor go away but it comes back when you click in the text input box?

A: 

.focus() can't determine the cursor position, for example where should the cursor be when you fire the focus event on the element? It's determined by where you click, which is a different event altogether.

Aside from that, when you .trigger() an event (.focus() is a shortcut for .trigger('focus')) it doesn't replicate the event the same as it would happen if the user created the event, e.g. the default action....for example the cursor position setting (.click() won't work either, for this same reason). The jQuery docs for .trigger() cover this briefly:

While .trigger() simulates an event activation, complete with a synthesized event object, it does not perfectly replicate a naturally-occurring event.

You can stop the default action (with event.preventDefault()), but there is no mechanism in jQurey core for creating or executing it.

Nick Craver