Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum call stack size exceeded on trigger click [duplicate]

I really want to do something simple. On click on a certain element, I trigger a click on another element but I get the below error on my console.

Uncaught RangeError: Maximum call stack size exceeded

My code is as below;

$('body').on('click', '.actual-click-element', function(event) { 
    $('.trigger-click-element').trigger('click');
    event.preventDefault(); 
});

I wonder why am am getting this error and I don't see how this is recursive. Any ideas?

like image 837
Joe Ng'ethe Avatar asked Sep 21 '16 15:09

Joe Ng'ethe


People also ask

How do I fix error in maximum call stack size exceeded?

The call stack is limited in size, and when it's exceeded, the RangeError is thrown. This can happen when a deeply nested function is called, or when a lot of new variables are created. The most common way to fix this error is to reduce the number of function calls, or to limit the number of variables that are created.

What does it mean when maximum call stack size exceeded?

The JavaScript exception "too much recursion" or "Maximum call stack size exceeded" occurs when there are too many function calls, or a function is missing a base case.

How do I fix maximum call stack size exceeded see JavaScript console for details?

The "RangeError: Maximum call stack size exceeded" error occurs when a function is called so many times that the invocations exceed the call stack limit. To solve the error, specify a base case that has to be met to exit the recursion.

How do I fix max call stack size exceeded in jquery?

The most common source for this error is infinite recursion. You must have a recursive function in your code whose base case is not being met and is, therefore, calling the function again and again until you hit the call stack limit.


1 Answers

Surely because .trigger-click-element is descendant of .actual-click-element...

To avoid recursive call, you could use jq triggerHandler():

Events triggered with .triggerHandler() do not bubble up the DOM hierarchy; if they are not handled by the target element directly, they do nothing.

$('body').on('click', '.actual-click-element', function(event) { 
    $('.trigger-click-element').triggerHandler('click');
    event.preventDefault(); 
});

Now if $('.trigger-click-element') returns more than one element, you could use:

$('.trigger-click-element').each(function(){$(this).triggerHandler('click');});
like image 109
A. Wolff Avatar answered Oct 04 '22 07:10

A. Wolff