Posted By

Meander365 on 01/24/10


Tagged

javascript select input forms jquery button tabs textarea tab


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

nightshiftc


Set Focus to the Next Input Field with jQuery


 / Published in: jQuery
 

URL: http://jqueryminute.com/set-focus-to-the-next-input-field-with-jquery/

I was recently faced with the problem of setting focus to the next input field. The challenge was that I didn’t know what that field was. So given an input field, find the next logical (in the order of the DOM) input field and set focus. I came up with the following jQuery function (plugin) to accomplish this:

  1. $.fn.focusNextInputField = function() {
  2. return this.each(function() {
  3. var fields = $(this).parents('form:eq(0),body').find('button,input,textarea,select');
  4. var index = fields.index( this );
  5. if ( index > -1 && ( index + 1 ) < fields.length ) {
  6.  
  7. fields.eq( index + 1 ).focus();
  8. }
  9. return false;
  10. });
  11. };

Report this snippet  

You need to login to post a comment.