So I am having a problem with something that I think should be very simple.
I coded this example for simplicity's sake: I first noticed the problem in a larger body of code. However the issue still persists here.
<!DOCTYPE html>
<html>
<head>
<style>
#test
{
height: 300px;
width: 300px;
background: blue;
position:absolute;
top: 0px;
left: 0px;
}
</style>
</head>
<body>
<div id="test"></div>
<script src='jquery-1.10.2.min.js'>
</script>
<script>
var isClicked = false;
$(document).ready(function(){
$('#test').click(function(){
isClicked = true;
});
if (isClicked == true)
{
$('#test').animate({top:'200px'});
}
});
</script>
</body>
</html>
The animation is not playing... obviously the same task could be accomplished in an easier way, but I still don't understand why the variable is not working. Probably just a stupid typo - but maybe not.
Thanks for the help!
$('#test').click(function(){ if (isClicked) { $(this).animate({top:'200px'}); } isClicked = !isClicked; });isClicked(==true),isClickedisn't set. The code doesn't know it yet.isClickedtotrueand nothing else. Put thatif (...) {...}inside the function of the click.