Solved

Too much recursion in javascript

Posted on 2009-06-29
5
790 Views
Last Modified: 2012-06-21
I wrote a little javascript snippet to hide/show a div when a checkbox is unticked/ticked. Please see the code snippet for my code.

I know I've gone wrong somewhere because FireBug in FireFox gives me a "too much recursion" error. But I don't know enough about javascript to see where I've gone wrong.
<script type="text/javascript" charset="utf-8">
$(function showdiv(check_name){
	$(check_name).toggle(150);
	return false;
});
</script>
 
<p class="userdisp_alt"><input type="checkbox" class="user_check" name="fld_FirstName_check" onclick="showdiv('#fld_FirstName')"/><label for="fld_FirstName">First Name</label></p>
<p id="fld_FirstName" class="udf_textbox"><input type="text" name="fld_FirstName" /></p>

Open in new window

0
Comment
Question by:synergiq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 29

Expert Comment

by:Badotz
ID: 24735318
I don't see where any HTML element has an id of "#fld_FirstName". I see "fld_FirstName", so perhaps this is your problem?
0
 
LVL 2

Author Comment

by:synergiq
ID: 24735621
If I change it from onclick="showdiv('#fld_FirstName')" to onclick="showdiv('fld_FirstName')" then the toggle stops working.

At the moment, the javascript works but in IE I get a 'Stack Overflow' error and in FireFox it says that there is too much recursion. I've removed this function from the page and although the toggle stops working the stack overflow error goes away so I'm sure it's this function that is causing it.

I've googled the problem and I'm getting results like this: http://stackoverflow.com/questions/639862/too-much-recursion-error-in-jquery-1-3-2 saying that it is a problem with the javascript code but I am unsure what I have to change to make it work.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 24735693
Not being familiar with jquery, I would suggest delving into the documentation to be sure you are using the correct syntax.
0
 
LVL 10

Accepted Solution

by:
jfromanski earned 500 total points
ID: 24736140
In the way you put it into code the function showdiv is executed during page load without knowing what check_name is - hence the error. Try
<script type="text/javascript" charset="utf-8">

function showdiv(check_name){
        $(check_name).toggle(150);
        return false;
}
</script>

0
 
LVL 2

Author Comment

by:synergiq
ID: 24736567
That is perfect, thank you!
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question