I am trying to build a very simple web application.
I want the values of one textfield to be stored after I press a button, and to be added.
For example, if I typed 3, clicked submit, then 5, clicked submit, and then 2, I would like the result to be 10, but I'm not sure how to do this.
<style media="screen">
.error {
display: none;
}
.number {
display: none;
}
</style>
<body>
<form>
<label for="numberInput">Write a number: </label>
<input type="text" name="numberInput" class="numberInput" id="numberInput">
<button type="button" name="submitBtn" class="submitBtn" id="submitBtn">Submit</button>
<div id="error" class="error">
</div>
<div id="number" class="number">
</div>
<script type="text/javascript">
$("#submitBtn").on("click", function() {
if ($("#numberInput").val() != "") {
$("#error").hide();
$("#error").text("");
var storedValue = $("#numberInput").val();
$("#number").show();
$("#number").text(storedValue);
} else {
$("#error").show();
$("#error").text("Please complete the above field.");
}
});
</script>
If I were to type 5 and then 2, I expect the result to be 7, however, the actual result that appears is 2.