Error jQuery: Uncaught Rangeerror: Maximum call stack size exceeded

Asked

Viewed 24,150 times

3

I have a simple function in jQuery, but it is showing the error:

Uncaught Rangeerror: Maximum call stack size exceeded

for example, there are 2 or 3 links I have on the page

<a id="islpronto_link" href="javascript:void(0)" class="bot botchat">Fale Conosco</a>

What is causing this error?

Down with my code:

$('.botchat').click(function(){
    $('#islpronto_link').click(); 
    return;
});

2 answers

14

You are the victim of a loop infinite.

Look. This is your code.

$('.botchat').click(function(){
    $('#islpronto_link').click(); 
    return;
});

Every time an element with the class botchat is clicked, it will be fired. Now see this part

$('#islpronto_link').click(); 

This calls the event click of the element that has Id islpronto_link. This causes your code to run again, and again, and again...

In practice, this is the same thing as doing

function funcao(){
    funcao();    
}

Notice that the code has no escape, the function always calls itself, infinitely.

-1

Guys I want to thank you very much for the help, I believe I found a way not far from what I had previously thought. follows an example code

<head>
   <script   src="https://code.jquery.com/jquery-2.2.4.min.js"   integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="   crossorigin="anonymous"></script>


    <meta charset="UTF-8">
    <script>
        $(function(){
            $(".bot").click(function(){

                $("#islpronto_link").click();
            });

        });

    </script>

    <title>teste</title>
</head>
<body>


<input type="button" class="bot" value="click">

<br/><br/>  
<a id="islpronto_link" class="" href="javascript:void(0)">   teste  </a>
<br/>
<a id="islpronto_link" class="" href="javascript:void(0)">   teste  </a>
<br/>   
<a id="islpronto_link" class="" href="javascript:void(0)">   teste  </a>
<br/>

again thank you so much!

  • 1

    Friend Ids can not be repeated, the only thing you did was to put the class in an input, seen by this angle it seems that you do not understand well yet DOM and Javascript, ie you are doing things randomly, on the kick. I recommend you review it well because this flaw, I’m just telling you why I want to help.

  • I get it, I’ll make it better. I’m grateful for the strength

  • Guilherme, I used the above logic just to understand how I would do, but I ended up doing otherwise, so I did not duplicate the Ids, but I know I need to study more JS, again I appreciate the help. strong hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.