people warn against passing a string to a setInterval like that. If you really need to pass the div to the function as an argument you can do this:
Code:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div style="height:100px;width:100px;background-color:red;"id="animate" onclick="flip(this)"></div>
<script type="text/javascript">
var thediv=document.getElementById("animate");
function flip(thediv){
var wid=parseInt(thediv.style.width);
if (wid==0){clearInterval(theint)}else{
wid--;
thediv.style.width=wid+"px"
}
}
theint=setInterval(function(){flip(thediv)},200);
</script>
</body>
</html>
otherwise you could just do this:
Code:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div style="height:100px;width:100px;background-color:red;"id="animate" onclick="flip()"></div>
<script type="text/javascript">
function flip(thediv){
var thediv=document.getElementById("animate");
var wid=parseInt(thediv.style.width);
if (wid==0){clearInterval(theint)}else{
wid--;
thediv.style.width=wid+"px"
}
}
theint=setInterval(flip,200);
</script>
</body>
</html>
Bookmarks