- news (347)
- links (345)
- jQuery Plugins (61)
- jQuery Code Snippets (34)
- jQuery and JavaScript (21)
- plugins (17)
- jQuery News (17)
- Uncategorized (16)
- Media (13)
- jQuery Mobile (11)
- jQuery and CSS (10)
- jQuery Utilities (9)
- jQuery and Ajax (9)
- Random jQuery Stuff (9)
- jQuery Functions (8)
- jQuery Articles (8)
- images (7)
- jQuery (7)
- jQuery Tutorials (7)
- jQuery and HTML (6)
- Javascript Libraries (6)
- jQuery and WordPress (6)
- jQuery Events (6)
- jQuery Testing (6)
- jQuery Image Scripts (5)
- jQuery Knob demo
- Magic Zoom : JavaScript image zoom tool
- jQuery Slider plugin
- jQuery Image Slider V3
- 翻訳:HTML5モバイルWebフレームワークの比較 - きしだのはてな
- Learning Advanced JavaScript
- Shadowbox.js Media Viewer
- Lazy Load Plugin for jQuery
- A Pure CSS3 Cycling Slideshow | Smashing Coding
- URL Decoder. Decode and parse URL online. Javascript jQuery plugin.
- minjs - Minimalist JavaScript
- 13 Great Flickr Galleries to Integrate into Your Site - The Designed
- jQuery Form Input Hints Plugin (Rob Volk's Blog)
- jQuery Mobile | jQuery Mobile
- Thickbox - ColorBox - FancyBox - ShadowBox Comparison - SoftwareProjects : Internet Marketing Services
- [5]テーブル、チャート、コード表示プラグインを活用する - みてわかる!jQuery入門:ITpro
- Comparing HTML5 Mobile Web Framework - Dzyngiri
- 20 More CSS3 Tutorials and Techniques for Creating Buttons
- Infragistics jQuery Controls – Impressive & Professional jQuery Toolset
- remybach/jQuery.superLabels - GitHub
- Tips and best practices to develop responsive websites | CatsWhoCode.com
- What’s new for designers, May 2012 | Webdesigner Depot
- dynamo.js
- 21 Fantastic Examples of Sliders in Web Design | Inspiration
- Edit this Fiddle - jsFiddle
Set Focus to the Next Input Field with jQuery
Setting focus to input fields is easy enough with JavaScript: document.getElementById(‘theInputField’).focus(); but sometimes you need a more generic solution as what happens when the next input field changes ID?
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 2 3 4 5 6 7 8 9 10 |
$.fn.focusNextInputField = function() { return this.each(function() { var fields = $(this).parents('form:eq(0),body').find('button,input,textarea,select'); var index = fields.index( this ); if ( index > -1 && ( index + 1 ) < fields.length ) { fields.eq( index + 1 ).focus(); } return false; }); }; |
The use is as follows:
$( 'current_field_selector' ).focusNextInputField();
Let’s break this code down some:
$.fn.focusNextInputField = function() {
Start off by adding a new jQuery function/plugin
return this.each(function() {
Given a set of elements (this => jQuery object), iterate over them (we’ll return false which stop iteration after the first element.)
var fields = $(this).parents('form:eq(0),body').find('button,input,textarea,select');
Walk up the DOM tree (parents) until we find either the first form element or reach the body tag. Now find all button,input,textarea and select elements.
var index = fields.index( this );
Find out if our current DOM element (this) is in the jQuery collection. Index will return -1 if it is not.
if ( index > -1 && ( index + 1 ) < fields.length ) {
See if we have a match and make sure we aren’t the last element
fields.eq( index + 1 ).focus();
Select the next input field ( index + 1 ) and set focus to it.
} return false;
Return false so we stop iteration after this element. So if you call $(…).focusNextInputField() with multiple elements, it will only set focus to the next input field of the first element.
Good luck & enjoy!
Read the full/original article at jQuery Minute™
Comments are closed.
Learn more about jQuery
I only just found out that the jQuery validation plugins has a validation rule called “remote” which can be used ...
jQuery function to check if horizontal scroll is present – hasHScrollBar() – (or vertical check below also, util function to ...
jQuery function to Set any DOM Element to Top View (bring to front) using CSS Z-Index property.
Is it possible to declare arrays in JavaScript object literal notation? Example 1 – this works with arrays Declaration: Storage ...
To get the a variables type using jQuery there is a jQuery function called .type() which returns “array”, “string”, “number”, ...
In this post you can find tutorials which explain step by step different API use cases with jQuery like Google ...
For valuable work on creation of sites you need a good comfortable editor necessarily. There are many requiring paid products ...
Today we are sharing with you a collection of awesome jQuery Camera Photo plugins. They offer a range of image ...
Back in the day, if you saw something that was animated on a website it was automatically assumed to be ...
In this post, we have compiled a list of 10 jQuery HTML5 Audio players available today, most allow native audio ...
jQuery Mobile is a powerful framework for making mobile web applications. But can we use it to convert existing desktop ...
I have jotted a quick post on a Basic JavaScript Regular Expression Example to give beginners out there a taste ...
In April 2012′s edition of Interestingly Random JavaScript, jQuery and Web Development we bring you some very cool stuff such ...
In this post we are sharing you a roundup of 10 really jQuery plugins as of today. Pretty cool plugins ...
As always expected from the jQuery community, the compilation we have for you today are some good and impressive recently ...
A collection of jQuery PNG/JPEG/GIF plugins that enables image animation, cartoon-like background, etc… let them help you design displays for ...
A collection of JavaScript/jQuery Zip/File/Compressor plugins that allows you to minify your JS code and compress your JS files ready ...
Quick jQuery code snippet on how to keep an element in view. For demo scroll down on any page on ...
Here is what I think is the best and most reliable way to load jQuery library and jQuery UI Libraries. ...
jQuery code snippet which outputs all attributes of element using the default .attr() function on any dom element(s). usage: output:
Use jQuery and the Google Maps API to create your own map application.
Super cool and easy way to get stylish tooltips with jQuery.
The right way to include the jQuery library in WordPress.
