473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

changing focus to next input element

Say I have two input elements and that I wanted to make it so that when
the first ones input was what it should be, the focus would
automatically be shifted to the next input element.

ie. something like...

<input onkeyup="if (this.value.length == this.maxlength)
document.forms[0].elements['part2'].focus()" value="whatever"
maxvalue="x" type="text" />
<input value="whatever" maxvalue="x" type="text" />

This, unfortunately, doesn't work for me because, in my case, I have no
way of knowing what the id attribute is going to be. So is there a way
I can change the focus to the next element, whatever that element is?

Jun 20 '06 #1
2 55176
yawnmoth wrote:
Say I have two input elements and that I wanted to make it so that when
the first ones input was what it should be, the focus would
automatically be shifted to the next input element.

ie. something like...

<input onkeyup="if (this.value.length == this.maxlength)
document.forms[0].elements['part2'].focus()" value="whatever"
maxvalue="x" type="text" />
<input value="whatever" maxvalue="x" type="text" />

This, unfortunately, doesn't work for me because, in my case, I have no
way of knowing what the id attribute is going to be. So is there a way
I can change the focus to the next element, whatever that element is?


You can either go to the next element in the DOM tree (which would be
very unreliable), the next element in the form's elements collection
(if there is a next element) or you could use the tabIndex property.

The following script uses the 'next form element' method, but you could
get the element's tabIndex property and find the element with the next
tab index if you think the 'next' element isn't the next in the form's
elements collection.

<script type="text/javascript">

function doNext(el)
{
if (el.value.length < el.getAttribute('maxlength')) return;

var f = el.form;
var els = f.elements;
var x, nextEl;
for (var i=0, len=els.length; i<len; i++){
x = els[i];
if (el == x && (nextEl = els[i+1])){
if (nextEl.focus) nextEl.focus();
}
}
}

</script>

<form action="">
<table>
<tr>
<td>
<input type="text" maxlength="5" value="1234"
onkeyup="doNext(this);">
</td>
<td>
<input type="text">
</td>
</tr>
</table>
</form>
You might be able to get the tabIndex property, then just set focus on
the tabIndex+1 element of the elements collection, but that might be
unreliable, you should test it thoroughly (it works in Firefox 1.5 & IE
6 at least):

function doNext(el)
{
if (el.value.length < el.getAttribute('maxlength')) return;

var nextEl = el.form.elements[el.tabIndex+1];
if (nextEl && nextEl.focus) nextEl.focus();
}
--
Rob

Jun 20 '06 #2

RobG wrote:
yawnmoth wrote:
<snip>
You might be able to get the tabIndex property, then just set focus on
the tabIndex+1 element of the elements collection, but that might be
unreliable, you should test it thoroughly (it works in Firefox 1.5 & IE
6 at least):

function doNext(el)
{
if (el.value.length < el.getAttribute('maxlength')) return;

var nextEl = el.form.elements[el.tabIndex+1];
if (nextEl && nextEl.focus) nextEl.focus();
}

Modifying that slightly suggests that tabIndex is always sorta being
assumed to be 0, initially, by both IE and Firefox:

<script type="text/javascript">

function doNext(el)
{
if (el.value.length < el.getAttribute('maxlength')) return;

var nextEl = el.form.elements[el.tabIndex+1];
if (nextEl && nextEl.focus) nextEl.focus();
}

</script>

<form action="">
<table>
<tr>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text" maxlength="5" value="1234"
onkeyup="doNext(this);">
</td>
<td>
<input type="text">
</td>
</tr>
</table>
</form>

I tried explicitly adding a tabindex="..." field to each of the input
elements, but the javascript seemed to stop working, entirely, after I
did that. I didn't get any javascript errors, either, so I'm kinda
confussed...

Any ideas?

Also, thanks for the ideas you've shared, already! :)

Jun 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: yabba | last post by:
I am building an asp form with various elements... <td tabindex=>blablabla</td> Where the is the order I want, so far so good and using the tab key provides the desired results however... ...
4
by: Paul Thompson | last post by:
How do I determine in JavaScript the name of the object that focus is on?
4
by: CharlesTheHawk | last post by:
I thought this would be really simple, but i'm stumped. I'd like to validate some data without submitting the form, but i can't seem to get the focus to go back to the field with the bad data. I've...
7
by: mcanedo | last post by:
Hi: I have several text elements in a form. Some of they are filled by the user, and then some are automatically filled based on the user input. Therefore I do not want the user to be able to...
6
by: MickG | last post by:
Hi, I am trying to validate these values, this seems to work fine for the phone number and name but I am trying to get the program to fail to submit and set the focus on the date when 2006 is...
8
by: horos | last post by:
hey all, Ok, a related question to my previous one on data dumpers for postscript. In the process of putting a form together, I'm using a lot of placeholder variables that I really don't care...
3
by: VA | last post by:
t=document.getElementById('mytable') is a HTML table with some input fields in its cells Why doesnt t.getElementsByTagName('tr').firstChild.focus; put the focus on that text field? It...
0
by: Vinod. | last post by:
Hi all, I have added browser control to a windows form. I am loading pages having multiple frames. I have noticed that the focus in a text is not maintained when the application is deactivated....
2
dlite922
by: dlite922 | last post by:
Before traversing my code, here's what my goal is and what this function does: I have a table of fields that dynamically grows as the user enters information. A minimum of 3 rows must always...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.