I am using the autocomplete plugin with jQuery and it is working fine. However, in IE, when the user selects an item in the autocomplete, the focus does not then move to the next input field. Naturally it works in Firefox. The plugin doesn't have a built-in solution but does provide for "options". Is there a way I can force it to move to the next input field?
- 1? What do you mean. Why should the focus move to the input field if you select something in the autocompleter?jitter– jitter2010-03-16 14:40:51 +00:00Commented Mar 16, 2010 at 14:40
- my "form" is actually a <table> and the next input is a <td> inside the same table. Yes, I need it to move to the next input after something is selected in the autocompleter.Matt– Matt2010-03-16 18:29:45 +00:00Commented Mar 16, 2010 at 18:29
- I know this is 12 years old, but I don't suppose you have any script to look at?jpgerb– jpgerb2022-05-23 18:13:44 +00:00Commented May 23, 2022 at 18:13
15 Answers
You can do something like this:
$("input").change(function() { var inputs = $(this).closest('form').find(':input'); inputs.eq( inputs.index(this)+ 1 ).focus(); }); The other answers posted here may not work for you since they depend on the next input being the very next sibling element, which often isn't the case. This approach goes up to the form and searches for the next input type element.
5 Comments
JQuery UI already has this, in my example below I included a maxchar attribute to focus on the next focus-able element (input, select, textarea, button and object) if i typed in the max number of characters
HTML:
text 1 <input type="text" value="" id="txt1" maxchar="5" /><br /> text 2 <input type="text" value="" id="txt2" maxchar="5" /><br /> checkbox 1 <input type="checkbox" value="" id="chk1" /><br /> checkbox 2 <input type="checkbox" value="" id="chk2" /><br /> dropdown 1 <select id="dd1" > <option value="1">1</option> <option value="1">2</option> </select><br /> dropdown 2 <select id="dd2"> <option value="1">1</option> <option value="1">2</option> </select> Javascript:
$(function() { var focusables = $(":focusable"); focusables.keyup(function(e) { var maxchar = false; if ($(this).attr("maxchar")) { if ($(this).val().length >= $(this).attr("maxchar")) maxchar = true; } if (e.keyCode == 13 || maxchar) { var current = focusables.index(this), next = focusables.eq(current+1).length ? focusables.eq(current+1) : focusables.eq(0); next.focus(); } }); }); 2 Comments
What Sam meant was :
$('#myInput').focus(function(){ $(this).next('input').focus(); }) 2 Comments
Try using something like:
var inputs = $(this).closest('form').find(':focusable'); inputs.eq(inputs.index(this) + 1).focus(); 2 Comments
why not simply just give the input field where you want to jump to a id and do a simple focus
$("#newListField").focus(); 2 Comments
Use eq to get to specific element.
Documentation about index
$("input").keyup(function () { var index = $(this).index("input"); $("input").eq(index + 1).focus(); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" maxlength="1" /> <input type="text" maxlength="1" /> <input type="text" maxlength="1" /> <input type="text" maxlength="1" /> <input type="text" maxlength="1" /> <input type="text" maxlength="1" /> Comments
Could you post some of your HTML as an example?
In the mean-time, try this:
$('#myInput').result(function(){ $(this).next('input').focus(); }) That's untested, so it'll probably need some tweaking.
1 Comment
I just wrote a jQuery plugin that does what you are looking for (annoyed that that I couldn't find andy solution myself (tabStop -> http://plugins.jquery.com/tabstop/)
Comments
if you are using event.preventDefault() in your script then comment it out because IE doesn't likes it.
1 Comment
Here is what worked in my case. Might be less performance intensive.
$('#myelement').siblings('input').first().focus(); 1 Comment
var inputs = $('input, select, textarea').keypress(function (e) { if (e.which == 13) { e.preventDefault(); var nextInput = inputs.get(inputs.index(this) + 1); if (nextInput) { nextInput.focus(); } } });