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?
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.
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.
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.
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.
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');});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With