JavaScript HTML5 and Javascript - a function appears to be working only once html5 javascript function,html5 javascript function parameters,html5 javascript function

guys, I am playing arround with HTML5 and javascript. The current thing which I am making is the following: there is a purple block on the screen and when you click a button it is moved 100 pixels to the right. This works so far, however, the function works only on the first time it is ran. I can't find my bug. I am posting the entire source code (javascript, html and css)

<!doctype html>
<head>
<style>
#stage{
    position: relative;
    width : 800px;
    height : 600px;
    background: #c0c0c0;
    border: 1px dashed black;
}

.coil{
    width: 64px;
    height: 64px;
    background:  #800080;
    position: absolute;
    top: 200px;
    left: 50px;
}
</style>
</head>


<body>
<div id="stage">
<div class="coil"></div>
<button id="button1">move!</button>
</body>
<script>
var coil = document.querySelector(".coil");
var button = document.querySelector("#button1");
button.addEventListener("click", clickHandler2, false);

//why is it working correctly just once
function clickHandler2()
{
    coil.style.left += 100 + "px";
}
</script>
Answer:1

As nycynik mentioned, you are being a bit careless with the string additions.

Try this:

function clickHandler2()
{
    var before = parseInt(coil.style.left);
    before = isNaN(before) ? 0 : before;
    coil.style.left = before + 100 + "px";
}
Answer:2

When you do your add like that, its not actually adding to the value, its only making a new string; add a console.log on the button and you will see.

console.log(coil.style.left += 100 + "px");

the output is "100px100px"

one alternative solution:

var coilPos = 100;
//why is it working correctly just once
function clickHandler2()
{
    coilPos += 100;
    coil.style.left = coilPos + "px";
    console.log(coil.style.left += 100 + "px");
}
Answer:3

I am trying to create a javascript algorithm for an ul menu that stretches out li elements to use the available width (100%). I was thinking about an algorithm with this flow: 1. calculate the ...

I am trying to create a javascript algorithm for an ul menu that stretches out li elements to use the available width (100%). I was thinking about an algorithm with this flow: 1. calculate the ...

Below is the code where I am disabling ENTER button. $(document).ready(function () { $(window).keydown(function (event) { if ((event.keyCode == 13) && ($('#btnsubmit').disabled)) ...

Below is the code where I am disabling ENTER button. $(document).ready(function () { $(window).keydown(function (event) { if ((event.keyCode == 13) && ($('#btnsubmit').disabled)) ...

I want three lines of text to appear in a succession, then hide, and then appear in succession again. So far, I have been able to split the lines into span tags and appear in succession, but I can't ...

I want three lines of text to appear in a succession, then hide, and then appear in succession again. So far, I have been able to split the lines into span tags and appear in succession, but I can't ...

I have an object and a for in loop that looks like this: var golom = { MRTU03: { Temp: "10", Sp: "11" }, MRTU09: { Temp: "8", Sp: "7" } }; for (var f ...

I have an object and a for in loop that looks like this: var golom = { MRTU03: { Temp: "10", Sp: "11" }, MRTU09: { Temp: "8", Sp: "7" } }; for (var f ...

  1. object inside object javascript
  2. object inside object java
  3. object inside object python
  4. object inside object json
  5. object inside object c#
  6. object inside object php
  7. object inside object typescript
  8. object inside object swagger
  9. object inside object mongodb
  10. object inside object jquery
  11. object inside object scala
  12. object inside object json java
  13. object inside object
  14. object inside object angular
  15. object inside object react
  16. object inside object angular 2
  17. object inside object gson
  18. access object inside object javascript
  19. add object inside object javascript
  20. create object inside object javascript