I’m trying to build a function that will slide down and up the specific div I’ll mention later in the script, here’s the code:
<!DOCTYPE html>
<html>
<head>
<style>
div { background:yellow; border:1px solid #AAA; width:80px; height:80px; margin:0 5px; float:left; }
div.colored { background:green; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<button id="run">Run</button>
<div></div>
<div id="mover"></div>
<div></div>
<script>
$("button#run").click(function(){
$("div:animated").toggleClass("colored");
});
function animateIt() {
return $(this).slideToggle(5000, animateIt);
}
$("div#mover").animateIt();
</script>
</body>
</html>
But it gives me this error:
"Uncaught TypeError: Object [object Object] has no method ‘animateIt’"
animateItis not a jQuery method. Call it as a regular function, and pass in the element:Here’s your fiddle, updated: http://jsfiddle.net/ZcQM7/2/
If you want it to be a jQuery method, you’ll have to turn it into a plugin:
Here’s your fiddle again, with another update: http://jsfiddle.net/ZcQM7/1/