Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

interchangeably alter two html input form values using jquery [closed]

I have two input's on a form that i'd like to be able to interchangeably alter their variable values so that if input one's variable was the current price of Bitcoins, and input two's variable was USD's: as I typed out an amount of Bitcoins in input one, it'd update the price of USD in input two, or if i typed out a price in USD in input two it would change the value of bitcoins in input one.

How would i accomplish this with jquery//javascript.

like image 277
user3104155 Avatar asked Feb 02 '26 01:02

user3104155


1 Answers

Here's a simple example based on a 1:800 exchange rate:

HTML

<form>
    <label for="bitcoin">Bitcoin</label>
    <input type="text" id="bitcoin" value="1">
    <br>
    <label for="usd">USD</label>
    <input type="text" id="usd" value="800">
</form>

JavaScript (requires jQuery)

$(document).ready(function() {

    $('#bitcoin').keyup(function(e) {
        var val = $(e.target).val();
        if( val == undefined ) {
            $('#usd').val("");
        }
        else {
            $('#usd').val(val * 800);
        }
    });

    $('#usd').keyup(function(e) {
        var val = $(e.target).val();
        if( val == undefined ) {
            $('#bitcoin').val("");
        }
        else {
            $('#bitcoin').val(val / 800);
        }
    });

});

Here it is on jsFiddle.

like image 131
seancdavis Avatar answered Feb 03 '26 13:02

seancdavis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!