The focus event on the div does not work as expected

I have the following jQuery code:

$("#contact-form-applet").on("focusout",".dc2-ltfc", {ctx:this},function(event) {
    var curRowDiv = $(this);
    var rowObject = {};
    curRowDiv.find("input, textarea").each(function(e){
        rowObject[ $(this).attr("aria-label") ] = $(this).val();
    });
    console.log(rowObject);
}); 

The problem is that the code runs every time a field loses focus (field layout), and not a full div that loses focus.

Here is a JS Fiddle link demonstrating the problem.

I would like to call the code only when a particular line of the div loses focus, so that I can run the update once, and not every time the field in the line is updated.

+3
source share
1 answer

The jquery documentation says the following:

A focus event is dispatched to an element when it or any element within it loses focus.

, focusout , - . body keyup click, , closest :

    var previousId=null;

    $('body').keyup(function (e) {
        var code = e.keyCode || e.which;

        if (code == '9') {
            focusChange(e);
        }

    });

    $('body').click(function (e) {
        focusChange(e);
    });

    function focusChange(e) {
        if (!previousId) {
            previousId = $(e.target).closest('.dc2-ltfcc').attr('id');
            return;
        }
        if (previousId != $(e.target).closest('.dc2-ltfcc').attr('id')) {
            console.log('Exit');
            previousId = null;
        }
    }

, .

0

All Articles