0
How can I make this such that the red box moves on clicking the start button?
I tried this code but it moves anyways. https://code.sololearn.com/W37DR9Mvkk6s/?ref=app
2 Respuestas
+ 1
Change your code like this
HTML
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div id="container">
<div id="box"> </div></div>
<button id="demo" onclick="start()">Start</button>
</body>
</html>
//Javaacript
function start() {
//our box element
var pos=0;
var box = document.getElementById('box');
var t = setInterval(myFunction, 10);
var btn = document.getElementById("demo");
function myFunction() {
if(pos >= 150) {
clearInterval(t);
}
else {
pos += 1;
box.style.left = pos+'px';
}
}
};
+ 1
Shobhith J B thanks