3
votes

Why I am getting this error:

Uncaught RangeError: Maximum call stack size exceeded

Here is my code:

$(document).on('keypress focusout', '.checklist-item-input', function (e) {
  if (e.which == 13 || e.type == 'focusout') {
    $('.checklist-item').removeClass('edit');
    $(this).siblings('.checklist-item-detail').text($(this).val());
    $(this).blur();
    $('.checklist-item-detail').each(function () {
      if (!$(this).text().length) {
        $(this).closest('.checklist-item').parent().remove();
      }
    });
  }
});
1
'Cause on focusout you call .blur() which raises focusout which calls .blur() which raises focusout which calls .blur() which raises focusout which calls .blur() which raises focusout which calls .blur() which raises focusout which calls .blur() .... etc - freedomn-m
$(this).blur(); executes the focusout event, infinite times called - Kevin Kloet

1 Answers

1
votes

As others mentioned you effectively cause a recursive call. One simple fix is to add a sentinel variable to stop it going recursive:

var busy = false;
$(document).on('keypress focusout', '.checklist-item-input', function (e) {
  if (!busy && e.which == 13 || e.type == 'focusout') {
    busy = true;
    $('.checklist-item').removeClass('edit');
    $(this).siblings('.checklist-item-detail').text($(this).val());
    $(this).blur();
    $('.checklist-item-detail').each(function () {
      if (!$(this).text().length) {
        $(this).closest('.checklist-item').parent().remove();
      }
    });
    busy = false;
  }
});