PDA

View Full Version : Is there a way to tab the focus?


rrl
02-11-2003, 02:37 PM
I am building a screen of various user inputs. Based on conditions that involve a combination of the user's inputs, I may be disabling some input fields, requiring an entry on others, or making some input fields optional. I put this logic in one function and it works fine. The input fields were all defined with a tabindex. Most input fields have an onChange defined which all runs my function.

Everything works fine... except positioning focus to the next input field. Is there a way to simply call a function to tab to the next input -or- must I track each and every field that calls my function and code all the possible next input posibilities?

Any ideas would be appreciated
-Ron

Stoffel
02-11-2003, 03:52 PM
<script language="JavaScript">
function GotoNext() {
document.form.txt2.focus() }
</script>


<form name="form">
<input type="text" name="txt1" onFocus="GotoNext()">
<input type="text" name="txt2">
</form>

This should work

beetle
02-11-2003, 03:56 PM
Well, in Mozilla you can probably simulate a Tab-Key press, but that's hardly ideal.

Well, you could use the elements collection to do this.function goNext( elem )
{
var f = elem.form;
var i = 0;
while ( f.elements[i].name != elem.name )
i++;
f.elements[++i].focus();
}

arnyinc
02-11-2003, 03:58 PM
I might have misunderstood what you're trying to do, but this will determine if a field is disabled and skip it.

<html>
<head>
<script language="JavaScript">
function checkdisabled(currfield, fname) {
if (currfield.disabled)
fname.elements[next].focus();
}
</script>
</head>

<body>
<form name="myform">
first name: <input type="text" name="first_name" onFocus="checkdisabled(this, this.form)"><br>
last name: <input type="text" name="last_name" onFocus="checkdisabled(this, this.form)"><br>
phone: <input type="text" name="phone" disabled onFocus="checkdisabled(this, this.form)"><br>
address: <input type="text" name="address" onFocus="checkdisabled(this, this.form)"><br>
<input type="submit" name="submit">
</form>
</body>
</html>