0

I have an input tag and I want to take its value and put it in a span after a simple calculation. I did it but the value update only when the user hit the "Enter" button. Is there any way to do it dynamicaly? Without pushing the button.

<div class="col-sm-6 col-lg-6 col-md-6">
   Number:<input id="number" type="int" onchange="updateInput(this.value)">
</div>
<div class="col-sm-6 col-lg-6 col-md-6">
   Total <span id="result"></span>
</div>

<script type="text/javascript">
  function updateInput(ish){
    document.getElementById("result").innerHTML = ish*0.05
}
</script>
0

2 Answers 2

4

j08691's answer will work, but it won't include all ways that the user can update the value (such as pasting it). This should cover everything:

$("#number").bind("propertychange change keyup paste input", function () {
    document.getElementById("result").innerHTML = $("#number").val()*0.05;
});
Sign up to request clarification or add additional context in comments.

Comments

1

The easiest solution would be to change:

onchange="updateInput(this.value)"

to

onkeyup="updateInput(this.value)"

jsFiddle example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.