Automatically update text field depending on input field

I have these two input fields.

<input type="text" name="ltc">
<input type="text" name="btc" readonly>

Ok, now I want ... that when I enter some value in the first text box , then that value will be multiplied by a constant number and the result will be displayed in btc .

I think this can be done using jQuery, but I don't know how to do it (due to my limited / no knowledge in jQuery). Can anybody help me?

Thanks.

+4
source share
1 answer

Fast decision:

html code:

<input type="text" name="ltc" id="input-ltc">
<input type="text" name="btc" readonly id="input-btc">

JavaScript:

var inputLtc = document.getElementById('input-ltc'),
inputBtc = document.getElementById('input-btc');

var constantNumber = 2;

inputLtc.onchange = function() {
   var result = parseFloat(inputLtc.value) * constantNumber;
   inputBtc.value = !isNaN(result) ? result : '';
};

jsfiddle: http://jsfiddle.net/6KT4R/

edit:

You can use onkeydown to get the result as you type.

+4

All Articles