I'm very new to JavaScript and jQuery and I'm having trouble with a bit of code.
HTML:
<div class="toggle" style="display: block; width: 200px; height: 200px; background-color: red;">test</div>
JavaScript:
jQuery(document).ready(
function()
{
jQuery(".toggle").on("click", function() {
console.log("let the toggling begin!");
jQuery(this).slideToggle(600, function(){ // slide up
setTimeout(function(){ // wait 4 sec, then slide back down
jQuery(this).slideToggle(600)
}, 4000);
});
});
}
);
So the idea is that you click on the div, it slides up, then 4 seconds later slides back down. It doesn't work.
JSFIDDLE: http://jsfiddle.net/zEqN9/2/
However, if I change the this
inside each of the closures to ".toggle"
, then it does work.
JSFIDDLE: http://jsfiddle.net/YZxMb/
So clearly the issue is my use of this
.
I tried passing this
as a parameter into each of the two closure functions, but that gave the error Unexpected token this
.
How can I access the this
variable from the inner functions?