0

When I click on a DIV I need to press the TAB key (in order to go from one input field to the next one).

I tried this code but is not working:

$("#change_logo").click(function() { e = jQuery.Event("keydown"); e.which = 9; // This is the ID of the input where I want to simulate the "Press TAB Key" // and then I will go automatically to the following input which ID is // #newsletter_section_title2 $("#newsletter_section_title1").trigger(e); }); 

I know there are so many different ways to do this, .focus(), etc. How ever, for another reasons I need to do it simulating a TAB key pressed (but the user don't press TAB, the user only click on a div).

Anyone knows how to do it?

Many thanks, Dani

2 Answers 2

2

Simulating a tab press won't do the job here, it's not that you're doing anything incorrect, it's that simulating an event doesn't often invoke the default action, in this case focusing the next field. For example .click() on a <a> won't follow the link either.

You need to go the .focus() route, $('#newsletter_section_title2').focus(), to get the effect you want here...what are your reasons for avoiding it to begin with?

Sign up to request clarification or add additional context in comments.

Comments

0

I created a simple jQuery plugin which does solve this problem. It uses the ':tabbable' selector of jQuery UI to find the next 'tabbable' element and selects it.

Example usage:

$("#change_logo").click(function() { $.focusNext(); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.