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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With