Govan
Govan

Reputation: 2129

How to determine which element getting focus in ie 11

I want to focusout event in (Chrome, Ie 6-10) or in blur event in Firefox know which elements is going to get focus. In the chrome and IE before 11 it is possible using below code:

$(input).focusout(function (event) {
            "use strict";
            console.log("next focused element: ", event.relatedTarget);
        });

In Firefox it is possible using the below code:

$(input).blur(function (event) {
            "use strict";
             console.log("next focused element:",  event.originalEvent.explicitOriginalTarget);
            });

The problem is IE 11. I can not find any way to do that in IE 11. Somebody who has found a solution for this?

Here is fiddle for testing with ie11. http://jsfiddle.net/govanm/y87sekzd/5/

Upvotes: 3

Views: 3274

Answers (1)

Alvaro Montoro
Alvaro Montoro

Reputation: 29655

I could see the issue on the JSFiddle that you shared in the comments, and I was able to come up with a workaround solution.

The idea of this workaround is that before the focusout happens, some select/focus event triggers in the target element. So we can keep track of that by using some auxiliary variable:

var nextElement = null;

$("input").on("select click mousedown pointerdown mspointerdown", function() {
    nextElement = this;
});

I used different events that will be triggered when an element is selected (using the mouse or the keyboard) and that happen before the focusout event.

Now that we have the nextElement, on the focusout event of the current element, we'll check the value of event.relatedTarget. If it's not null (all but IE11), we'll use it; if it's null (IE11), we'll use the nextElement variable instead:

var nextFocus =  event.relatedTarget ? event.relatedTarget : nextElement;

$("#p1").append("<div>next focused element: " + nextFocus.id + "</div>");

That way we won't affect the behavior on the other browsers, and it will work once IE11 fixes the know bug that you shared in the comments.

The whole code would look like this:

var nextElement = null;

$("input").on("select click mousedown pointerdown mspointerdown", function() {
  nextElement = this;
});

$("input").focusout(function (event) {

  "use strict";

  var nextFocus =  event.relatedTarget ? event.relatedTarget : nextElement;

  $("#p1").append("<div>next focused element: " + nextFocus.id + "</div>");

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="group">
    <tbody>
    <tr></tr>
    <tr>
        <td class="field_title">
            <div class="widget_title widget_committed"><span id="sales_responsbile_part_last_name_field-title">* Last name:</span>
            </div>
        </td>
        <td class="field_widget">
            <div class="configurationWidget_configuration-custom-text-widget_0">
                <div class="widget_input"><input type="text" name="sales_responsbile_part_last_name_field" value="gf" id="in1">
                </div>
            </div>
        </td>
        <td class="field_filler"></td>
    </tr>
    <tr>
        <td class="field_title">
            <div class="widget_title widget_committed"><span id="sales_responsbile_part_first_name_field-title">* First name:</span>
            </div>
        </td>
        <td class="field_widget">
            <div class="configurationWidget_configuration-custom-text-widget_0">
                <div class="widget_input"><input type="text" name="sales_responsbile_part_first_name_field" value="hj" id="in2">
                </div>
            </div>
        </td>
        <td class="field_filler"></td>
    </tr>
    <tr>
        <td class="field_title">
            <div class="widget_title widget_committed"><span id="sales_responsbile_part_title_field-title">Title:</span>
            </div>
        </td>
        <td class="field_widget">
            <div class="configurationWidget_configuration-custom-text-widget_0">
                <div class="widget_input"><input type="text" name="sales_responsbile_part_title_field" value="hj" id="in3"></div>
            </div>
        </td>
        <td class="field_filler"></td>
    </tr>
    <tr>
        <td class="field_title">
            <div class="widget_title widget_committed"><span
                    id="sales_responsbile_part_phone_field-title">* Phone:</span></div>
        </td>
        <td class="field_widget">
            <div class="configurationWidget_configuration-custom-text-widget_0">
                <div class="widget_input"><input type="text" name="sales_responsbile_part_phone_field" value="jhjh" id="in4">
                </div>
            </div>
        </td>
        <td class="field_filler"></td>
    </tr>
    <tr>
        <td class="field_title">
            <div class="widget_title widget_committed"><span
                    id="sales_responsbile_part_mobile_field-title">Alt. phone:</span></div>
        </td>
        <td class="field_widget">
            <div class="configurationWidget_configuration-custom-text-widget_0">
                <div class="widget_input"><input type="text" name="sales_responsbile_part_mobile_field" value="hjh" id="in5">
                </div>
            </div>
        </td>
        <td class="field_filler"></td>
    </tr>
    <tr>
        <td class="field_title">
            <div class="widget_title widget_committed"><span
                    id="sales_responsbile_part_email_field-title">* E-mail:</span></div>
        </td>
        <td class="field_widget">
            <div class="configurationWidget_configuration-custom-text-widget_0">
                <div class="widget_input"><input type="text" name="sales_responsbile_part_email_field" value="hjh" id="in6">
                </div>
            </div>
        </td>
        <td class="field_filler"></td>
    </tr>
    <tr>
        <td class="field_title">
            <div class="widget_title widget_committed"><span id="sales_responsbile_part_sales_channel_field-title">Sales channel:</span>
            </div>
        </td>
        <td class="field_widget">
            <div class="configurationWidget_configuration-custom-text-widget_0">
                <div class="widget_input"><input type="text" name="sales_responsbile_part_sales_channel_field"
                                                 value="hjh" id="in7"></div>
            </div>
        </td>
        <td class="field_filler"></td>
    </tr>
    <tr>
        <td class="field_title">
            <div class="widget_title widget_committed"><span
                    id="sales_responsbile_part_sales_responsbile_segment_field-title">Sales responsible segment:</span>
            </div>
        </td>
        <td class="field_widget">
            <div class="configurationWidget_configuration-custom-text-widget_0">
                <div class="widget_input"><input type="text"
                                                 name="sales_responsbile_part_sales_responsbile_segment_field"
                                                 value="jhjh" id="in8"></div>
            </div>
        </td>
        <td class="field_filler"></td>
    </tr>
    <tr>
        <td class="field_title">
            <div class="widget_title widget_committed"><span id="sales_responsbile_part_sales_team_field-title">Sales team:</span>
            </div>
        </td>
        <td class="field_widget">
            <div class="configurationWidget_configuration-custom-text-widget_0">
                <div class="widget_input"><input type="text" name="sales_responsbile_part_sales_team_field" value="hjh" id="in9">
                </div>
            </div>
        </td>
        <td class="field_filler"></td>
    </tr>
    </tbody>
</table>
 <p id="p1">Test</p>

And you can see it on this JSFiddle: http://jsfiddle.net/y87sekzd/7/

Upvotes: 1

Related Questions