Forum Moderators: open
i have a html page with a form and a input text field. I've set the page to auto-focus on the textfield on load. however, if someone left or right clicks the mouse anywhere outside the textfield, then the focus is gone.
can anyone please tell me how i can use a javascript to re-focus on the text field if they click any mouse button out of the textfield?
many thanks
var message="NOT ALLOWED";
function click(e) {
if (document.all) {
if (event.button == 2) {
alert(message);
return false;
}
}
if (document.layers) {
if (e.which == 3) {
alert(message);
return false;
}
}
}
if (document.layers) {
document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown=click;
but instead of displaying the alert(message) i want it to focus on a form field. so i added the following underneath the alert(message):
document.forms.LoginForm.username.focus();
but this only seems to focus on the field every other time.
any ideas what i can do? thanks
Of course, you may want to ask yourself how your users will feel about this. I have a feeling if I was clicking away from an input field, I'd be either confused and/or frustrated if the focus kept going back to that input field.