tags:

views:

77

answers:

4

How do I multiple the value of one input by 2 and save it to another input?

So when I type 10 (for example), the value of the 2nd input will be 20?

Thanks

+1  A: 
var orgNo = $('#textInput').val();
var newNo = org * 2;
$('#textOutput').val(newNo);
Thomas Clayson
I think you meant `* 2` there, not `* 10`; also, `new` is a reserved word.
Piskvor
haha... its much too early. :p
Thomas Clayson
+1  A: 

I think this is what you are looking for:

$('#input-2').val($('#input-1').val() * 2);
elusive
+1  A: 

Assuming a structure like this:

<input id="inputfield1" type="text" />
<input id="inputfield2" type="text" />

you could use this code:

$("#inputfield1").keyup(function() { // when key is released in "#inputfield1"
// "change()" is also possible instead of "keyup()", slightly different behavior

    var input_value = parseFloat($("#inputfield1").val()); // get number as float
    // alternately parseInt(string, 10), in case you work with integers

    if (!isNaN(input_value)) { // the input is a number
        $("#inputfield2").val(input_value * 2); // update second field
    } else { // the input wasn't a number
        $("#inputfield2").val("not a number?"); // show an error mesage
    }
});
Piskvor
+1 for validation
Marko
but I need when typing second input is updated
miojamo
@user438755: Added the keyup event listener.
Piskvor
+3  A: 

Two input fields

<input type="text" id="input-value" />
<input type="text" id="output-value" readonly />

And some simple jQuery

var $output = $("#output-value");
$("#input-value").keyup(function() {
    var value = parseFloat($(this).val());
    $output.val(value*2);
});
Marko
keyUp is not a function?
miojamo
sorry try keyup(), I've edited my answer. http://api.jquery.com/keyup/
Marko
it's working thanks!
miojamo