P
please-answer-here
Why doesn't this script works?
I've tried different variant with focus 2 fields ahead or back which works
fine. But when I try to focus on the field I've just left I'm unable to
"refocus" on that field. My guess is that the combination of onchange and
focus() causes the problem, but I haven't got the big experience and
knowledge to wheter my assumption is right or if I just made a big blunder
somewhere.
Again I'll hope that someone could spare a few minutes in increasing my
(little) knowledge in javascript and DOM
<html>
<head>
<title>just a silly little test</title>
<script>
function validate(that) {
var xid=that.id;
var yid=that.form.id;
var xantelm=that.form.elements.length;
for (i=0;i<xantelm;i++) {
if (document.forms[yid].elements.id == xid) {
if (that.value.length!=3) {
alert('Exactly 3 characters are needed');
document.forms[yid].elements.focus();
}
}
}
return true;
}
</script>
</head>
<body>
<form id="forma" action="sletmig.asp" method="post">
<input id="fielda" type="text" onchange="validate(this);">
<input id="fieldb" type="text" onchange="validate(this);">
<input id="fieldc" type="text" onchange="validate(this);">
</form>
</body>
I've tried different variant with focus 2 fields ahead or back which works
fine. But when I try to focus on the field I've just left I'm unable to
"refocus" on that field. My guess is that the combination of onchange and
focus() causes the problem, but I haven't got the big experience and
knowledge to wheter my assumption is right or if I just made a big blunder
somewhere.
Again I'll hope that someone could spare a few minutes in increasing my
(little) knowledge in javascript and DOM
<html>
<head>
<title>just a silly little test</title>
<script>
function validate(that) {
var xid=that.id;
var yid=that.form.id;
var xantelm=that.form.elements.length;
for (i=0;i<xantelm;i++) {
if (document.forms[yid].elements.id == xid) {
if (that.value.length!=3) {
alert('Exactly 3 characters are needed');
document.forms[yid].elements.focus();
}
}
}
return true;
}
</script>
</head>
<body>
<form id="forma" action="sletmig.asp" method="post">
<input id="fielda" type="text" onchange="validate(this);">
<input id="fieldb" type="text" onchange="validate(this);">
<input id="fieldc" type="text" onchange="validate(this);">
</form>
</body>