Avicinnian
Avicinnian

Reputation: 1830

Focus an input field with javascript?

I've seen this done a few times before I think, but no explicit examples come to mind.

What I want to do is use javascript to toggle the currently selected form field. For example, I have a form field for a date of birth input, and one for an email. Once the date of birth is validated, the cursor will automatically switch to the email input field.

I would be using jQuery's change() event binder to monitor field changes and validate locally, and then execute the move on, I'm just not sure how to select a form field.

I may be wrong, and this may not be possible, but if it is, any help would be greatly appreciated as I've yet to find anything using the search queries I think would be most appropriate.

Thanks :).

Upvotes: 1

Views: 626

Answers (2)

Ilya Volodin
Ilya Volodin

Reputation: 11256

You are looking for the .focus() function. It will set a given element as selected (focused).

Upvotes: 0

ahren
ahren

Reputation: 16961

You can trigger the focus event.

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

You may find though, that the change event doesn't actually fire until the input field has lost focus, so you may need to listen to the keyup/paste events instead

Read more about .focus()

Upvotes: 3

Related Questions