> So you mean, you swapped 'price' and 'price2' in the JS, because you
> essentially want the reverse from what you are doing to happen?
At the end of the day, all I'm looking to do is to add validation to
my price field. If the validation fails, I want to select the text
and set the focus to that field. But this doesn't work:
if (document.getElementById("price").value == "xyz")
{
document.getElementById("price").focus();
document.getElementById("price").select();
}
When I tab out of the price field with "xyz", focus goes on to the
text area. But rewriting the javascript as follows, I can make the
focus go to Price2:
if (document.getElementById("price").value == "xyz")
{
document.getElementById("price2").focus();
document.getElementById("price2").select();
}
I've just tried to simplify the example so I can get an understanding
as to why it won't work. I'm stuck. :(
pr - 31 Mar 2008 16:24 GMT
> At the end of the day, all I'm looking to do is to add validation to
> my price field. If the validation fails, I want to select the text
[quoted text clipped - 8 lines]
> When I tab out of the price field with "xyz", focus goes on to the
> text area.
[...]
Whilst some consider it bad style to 'trap' a user in a text input
field, it should at least be possible to validate this way.
Unfortunately, this bug was reported to Mozilla in 2000 and hasn't been
fixed yet (<URL: https://bugzilla.mozilla.org/show_bug.cgi?id=53579>).
At least the workaround isn't too arduous. Here's a slight re-write of
your example:
Price <input type="text" id="price" value="xyz"
onblur="setMyFocus(this);"><br>
<!-- not <br/> - that would be XHTML -->
and
<script type="text/javascript"> <!-- not language -->
function setMyFocus(o) {
if (o.value == "xyz") {
window.setTimeout(function () {o.focus(); o.select();}, 0);
}
}
</script>
Note that the workaround has been around so long, it has a bug of its
own <URL: https://bugzilla.mozilla.org/show_bug.cgi?id=297134>. It would
be nice if the rise of Safari and a few more WebKit browsers finally
jolted Mozilla into fixing some of these antiques.