Display a JavaScript variable in an HTML form
Lycia-Garou
-
Lycia-Garou Posted messages 5 Status Member -
Lycia-Garou Posted messages 5 Status Member -
Hello,
Currently working on a big IT project with friends, we're facing a problem...
For creating via a form, the user can assign points to different categories. For this, I created a variable in JavaScript, it can be incremented or decremented via a button. However, I can't display it "in real-time" in an input or anything else...
In my head:
<script language="javascript">
var i = 0;
</script>
In my body, in my form:
<input type="button" id="incr" name="incr" onClick="i++;" value="+"/>
<input type="button" id="decr" name="decr" onClick="i--" value="-"/>
And I'm stuck... I've tried inputs, document.write, .innerHTML, I'm starting to mix everything up and I can't manage...
If anyone has an idea, I would be very grateful! Knowing that ideally, it's to send the value of variable i via the form (to store in a database).
Currently working on a big IT project with friends, we're facing a problem...
For creating via a form, the user can assign points to different categories. For this, I created a variable in JavaScript, it can be incremented or decremented via a button. However, I can't display it "in real-time" in an input or anything else...
In my head:
<script language="javascript">
var i = 0;
</script>
In my body, in my form:
<input type="button" id="incr" name="incr" onClick="i++;" value="+"/>
<input type="button" id="decr" name="decr" onClick="i--" value="-"/>
And I'm stuck... I've tried inputs, document.write, .innerHTML, I'm starting to mix everything up and I can't manage...
If anyone has an idea, I would be very grateful! Knowing that ideally, it's to send the value of variable i via the form (to store in a database).
3 answers
To place in <head> :
In the <form> :
<script type="text/javascript"> var i=0; function increase() { i++; document.getElementById('Counter').value= +i; } function decrease() { i--; document.getElementById('Counter').value= +i; } </script> In the <form> :
<form method="post" action=""> <p><input type="text" id="Counter" name="Score" value="0" /></p> <p><input type="button" value="Add" onclick="increase()" /> <input type="button" value="Remove" onclick="decrease()" /></p> </form>