Forum Moderators: open

Message Too Old, No Replies

left click and right click on mouse

         

ahmed24

7:52 am on Jun 23, 2009 (gmt 0)

10+ Year Member



hey everyone,

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

ahmed24

3:25 pm on Jun 23, 2009 (gmt 0)

10+ Year Member



i got the following script that returns an alert if the right mouse button is clicked:

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

whoisgregg

6:24 pm on Jun 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should be able use onblur() to focus back on the field. onblur() is fired when an element loses focus.

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.