Issue
Good Day, I'm trying to create a form calculate (a currency converter), in which when you are typing in the value of amount in USD, the other field is updated immediately without clicking any submit button. I have been searching but haven't gotten an answer (maybe due to searching for the wrong keywords).
An Example
<html>
<form action="">
<input type="number" name="amountUSD" value="1"><br/>
<input type="number" name="amountNGN" value="">
</form>
</html>
What I want to achieve is when the page loads, automatically, the value of amountNGN field should be 365 and of I remove the value of amountUSD field or make the value 0, the 365 in amountNGN field should go away or become 0.
Which makes the calculation: value(amountNGN) = value(amountUSD) * 356; (just an illustration, not sure this language exist).
The value of amountNGN field updates on the fly. Please how can I use JavaScript/jQuery to do this
Thanks
Solution
I believe this is what you want:
function convertCurrency(value) {
// your calculation here
return (value * 356);
}
$('[name="amountUSD"]').on('change keyup', function() {
value = $(this).val();
$('[name="amountNGN"]').val(convertCurrency(value));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="">
<input type="number" name="amountUSD" value="1"><br/>
<input type="number" name="amountNGN" value="">
</form>
Answered By - Matt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.