dimanche 30 novembre 2014

(beginner) HTML + JS change variables with input value


Hello Stackoverflow people,

My question is how to change a variable I assigned with JavaScript using the value of an HTML input tag.

my progress:



<script type="text/javascript">
var x = 0;
document.write(x);

function addtox() {
var addx = document.getElementById("plusx").value;
x = x + addx;
}

</script>

<input id="plusx" type="number">
<input type="button" onclick="addtox()" value="add">


The result is that it literally adds the value of id="plusx" to the 0 that's already there. So if the input would be 50, it would return 050, and not 50. If I repeat the button it returns 05050 in stead of 100.

How can I fix this?

Also: how can I update text that is already writting to the screen? x is already written to the screenbefore the change and doesn't update after it is assigned a new value.



p.s Sorry if I am a bit vague, not too familiar with coding questions like mine.





Aucun commentaire:

Enregistrer un commentaire