Search code examples
jqueryfocusjquery-events

jQuery focusout event repeating itself issue


This thing works, but each time I am clicking the element I want to get, it's "count" the clicks and then print log comments for the same number of total clicks.

For example, if I click the element 3 times in a row (and then clicking outside to trigger the focusout), it prints 3 log comments, if I then click 2 more times (and outside again) it prints 5 log comments.

And I want to get only one log comment each time when I am clicking outside the element.

Help anyone?

 $("#main-wrapper").click(function (e) {
    var contenteditable = $(e.target).attr('contenteditable');
    if (contenteditable == 'true') {
        $(e.target).focusout(function() {
            var content = $(this).html();
            console.log("get: " + content);
        });
    };          
});

Solution

  • Try unbinding the event.

    $("#main-wrapper").click(function (e) {
    var contenteditable = $(e.target).attr('contenteditable');
    if (contenteditable == 'true') {
        $(e.target).unbind('focusout').focusout(function() {
            var content = $(this).html();
            console.log("get: " + content);
        });
    };          
    });