Bug Tracker

Opened 6 years ago

Closed 5 years ago

#7311 closed bug (worksforme)

Blur() callback is not issued if the focus is changed from form.submit() callback

Reported by: alexalex Owned by:
Priority: low Milestone:
Component: event Version: 1.4.3
Keywords: Cc:
Blocked by: Blocking:

Description

Make a simple page with two inputs - #first and #second - and a Submit button:

 <form>
    <input id=first><br>
    <input id=second><br>
    <input type=submit>
  </form>

Then add this:

  $(document).ready(function(){

        $('form').submit(function(){
                $('input#second').focus();
                return false;
        });

        $('#first').blur(function(){
                alert('blur');
        });
  });

Then

  • (a) open this page in FF 3.6 on Windows
  • (b) click on first input to give it focus
  • (c) hit Enter to submit the form

The #second gets the focus, but there is no 'blur' alert.

The demo page - http://swapped.cc/tmp/jquery-blur-quirk.html

Change History (3)

comment:1 Changed 6 years ago by snover

  • Component changed from unfiled to event
  • Keywords needsreview added
  • Milestone 1.5 deleted
  • Priority changed from undecided to low

fiddle test case

So I am not actually sure this is a bug or not so I am leaving it open for review; you should probably be calling the focus function by doing $('input#second')[0].focus() which will ensure the appropriate events are dispatched.

Last edited 6 years ago by snover (previous) (diff)

comment:2 Changed 6 years ago by alexalex

Haaaa! That does solve the problem, thanks.

However the reason I logged it as a bug was that it was happening on Firefox only, but not on Chrome and IE.

Also, if you have a moment, can you explain how calling focus() on an array with one element is principally different from issuing the call directly for that element? It seems logical to assume that these two cases would yield the same result.

comment:3 Changed 5 years ago by dmethvin

  • Keywords needsreview removed
  • Resolution set to worksforme
  • Status changed from new to closed

I think snover's point was that $().focus() does *two* things. It fires a "focus" event starting on the target element that bubbles up the DOM, and (if no handler returns false or calls preventDefault) calls the element's DOM focus method. In this case you only want the latter, and the former may be interfering.

Note: See TracTickets for help on using tickets.