开发者

JQUERY Simulating Press the TAB key after User clicks on a DIV

开发者 https://www.devze.com 2023-01-10 12:47 出处:网络
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).

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 
   // #ne开发者_开发知识库wsletter_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


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?


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();  
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号