0

This code isn't working in Firefox but working on IE and Chrome. When I click inside the text box it runs the focusOut() and focusIn() functions. Is this behavior expected and is the syntax recommended?

<!doctype html>

<html>
  <head>
    <title>Page of Cage</title>
    <meta charset="UTF-8">
    <link rel="icon" href="mkx-logo.png" type="image/x-icon">
    <script type="text/javascript">
      function focusIn() {
        alert("focus In!");
      };

      function focusOut() {
        alert("focus OUT!");
      };
    </script>	
  </head>
  <body>
    <form>	
      Name:<input onfocus="focusIn()" onblur ="focusOut()" type="text" name="name"/> <br/>
      <input type="submit" value ="submit"/>			
    </form>
  </body>
</html>	

11
  • This should work on all browsers since everything looks correct to me. Commented Jul 11, 2015 at 3:22
  • 6
    Be careful about using alerts from focus event handlers, because the alert itself removes focus from the element. Commented Jul 11, 2015 at 3:24
  • 1
    @PatrickRoberts - Not really deprecated, don't forget in the new AngularJS you can still do ng-eventname ;) Commented Jul 11, 2015 at 3:27
  • 1
    This syntax isn't deprecated. Commented Jul 11, 2015 at 3:29
  • 1
    Try logging messages to the console instead using alerts. As @nnnnnn says, alerts gain focus and makes your input loses it, that is why it is firing both events. If you use console.log() instead alert , your input won't trigger blur event. Commented Jul 11, 2015 at 3:39

2 Answers 2

2

Replace alert with console.log and watch your web browser console using the developer tools. Just like nnnnnn said in the comments.

Sign up to request clarification or add additional context in comments.

Comments

0

Q:Why the code is not working?
A: onfocus is fired when focus is gained and onblur is fired when focus is lost. In your code when you focus on the textbox it generates onfocus and at the same time the alert box is generated and it has lost the focus. So, Both the function are launched.

Check out this Fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.