views:

16

answers:

1

How do I make for this onFocus to occur just 1 time (not as now that is blocking the windows with dialogs)?

<html>
    <form name="myForm">
      <input type="button"
             value="Big Button"
             name="myButton"
             onFocus="alert('Focus event occured')">
    </form>
    </html>

update:

sorry, when I say "a single time" I mean a single time each time it receive focus. Currently with a single focus it shows infinite dialogs.

+1  A: 

This is problematic. When the input field gets focus the focus event is fired, you handle that event by putting up an alert box which takes focus away (a "blur" event) from the input field. When the alert is dismissed the input field gets focus again, firing the focus even another time, and leading to an endless loop of gain/lose/gain focus. You are getting the focus even more than once because you are changing the focus.

You do not want to do something in a focus handler that changes the focus like this, unless it's something along the lines of "when this control gets focus, send focus to that other control instead".

If you do something that doesn't cause a focus change (which an alert does), such as adding text to a div when you get focus, you'll see that the event will only occur once.

If you describe what you're actually trying to accomplish you may get better advice. I assume the alert is just a test.

Stephen P
yes, the alert is just a test; I didn't realized that it was losing and regaining the focus... thanks!
Tom Brito