troubleshooting Question

jQuery .toggle() function question

Avatar of elepil
elepil asked on
jQuery
28 Comments1 Solution835 ViewsLast Modified:
A book I'm reading says "The toggle() function enables you to execute one of several functions, whenever a HTML element is clicked."

But when I run my example, something weird happens. The target HTML element somehow disappears!?? Can anyone tell me what's wrong?

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JQuery Test Page</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
	$("document").ready(function(e) {
            $("#myDiv1").toggle(function() {
                console.log("This is the first function");
            }, function() {
                console.log("This is the second function");
            });
	});
</script>
<body>
    <div id="myDiv1" class="contentDiv" style="background: #ccc;">
        This is Div1<br/>
        <ul>
            <li>John Doe</li>
            <li>Jane Doe</li>
        </ul>
    </div>
<div id="myDiv2" class="contentDiv">Div 2</div>
<div id="myDiv3" class="contentDiv">Div 3</div>

</body>
</html>

Also, the book further says that "You can pass as many functions to toggle() as you like (not just 2)." But when I enter a third function, I get an error from Netbeans that says "Uncaught TypeError: undefined is not a function".

Can anyone enlighten me on this?
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 28 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 28 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros