-5

I want to be able to alert the user if the object is at the bottom. What's the mistake i'm doing here?

If its easier to replace the "nummer var" and just check the top style-tag go ahead because it really doesn't matter! :)

Fiddle: http://jsfiddle.net/Bs5Jn/

Code:

<head>
<script src="jquery-2.1.0.min.js"></script>
<style>
#object{position:absolute;
width:100px;
height:100px;
background-color:yellow;}
</style>
</head>
<script>
var nummer = 0;

$(document).keydown(function(e) {
    switch (e.which) {
    case 38:
        $("#object").stop().animate({
            top: "10" 
        }); 
        nummer = 0;
        break;
    case 40:
        $("#object").stop().animate({
            top: "200"
        }); 
        nummer = 100;
        break;
    }
})
 if (nummer == 100) {
        //do something it does have the protected class!
        alert("the object is at the bottom");
    }

</script>
<body>
<div id="object">
</div>


</body>
user3332711
  • 31
  • 1
  • 4

1 Answers1

1

You should put the code inside the callback function, otherwise the nummer will just be 0.

$(document).keydown(function(e) {
    switch (e.which) {
    case 38:
        $("#object").stop().animate({
            top: "10" 
        }); 
        nummer = 0;
        break;
    case 40:
        $("#object").stop().animate({
            top: "200"
        }); 
        nummer = 100;
        break;
    }

   if (nummer == 100) {
        //do something it does have the protected class!
        alert("the object is at the bottom");
    }

})
xdazz
  • 158,678
  • 38
  • 247
  • 274