lovemysql
lovemysql

Reputation: 285

Moving an element with javascript

I am trying to move a div element with Javascript. I have used setInterval function and incremented the value. My code is:

<html>
<head>
<link rel="stylesheet" href="styles.css" type="text/css">
</head>
<button id="something"> click me </button>
<body>
<div id="insidename"><div>
<script>
var m = document.getElementById('something');
m.onclick = function() {
    var n = 0;
    window.setInterval(function() {
        var w = n++;
        document.getElementById('insidename').style.width = w;
    }, 2000);
}
</script>
</body>
</html>

my styles.css

#insidename {

    border: 2px solid #a1a1a1;
    padding: 10px;
    background: #dddddd;
    width: 15px;
}

I just want to make the progress bar move when i click the button ..But the code which i wrote isn't working

Upvotes: 0

Views: 71

Answers (1)

Barmar
Barmar

Reputation: 780994

The width style needs units at the end, such as px. So it should be:

document.getElementById('insidename').style.width = w+"px";

Upvotes: 4

Related Questions