Tasos
Tasos

Reputation: 7577

Update span text from input value

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>

Upvotes: 0

Views: 320

Answers (2)

freakinthesun
freakinthesun

Reputation: 699

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;
});

Upvotes: 4

j08691
j08691

Reputation: 207901

The easiest solution would be to change:

onchange="updateInput(this.value)"

to

onkeyup="updateInput(this.value)"

jsFiddle example

Upvotes: 1

Related Questions