2

Is there a way to update the value of a textbox on every key press?

I'm doing currency conversion. So, there are two textboxes, one for the user to input the value while the other will automatically show the converted amount on every key press.

Should I use a for loop? But what should I loop?

The textboxes in the following for loop should be able to do its individual row conversion

<% for(int i=0; i<2; i++) { %>
 <td><input type="text" id= "amount" name="amount<%=i%>" /></td>

 <td><input type="text" id= "convertedAmount" name="convertedAmount<%=i%>" readonly/></td>
<%}%>

How should I get started in attempting this task?

Thanks in advance for any possible help.

1
  • use keypress event myInput.addEventListener('keypress', handler) Commented Jun 14, 2013 at 3:03

2 Answers 2

6

onkeyup is best suited for your task:

Type amount $: <input type='text' id='amount' onkeyup="updateConverted()" >
<br>
Converted: <input type='text' id='converted' >

JavaScript function:

function updateConverted() {
    var conversionRate = 1.5;
    document.getElementById('converted').value =
                       document.getElementById('amount').value * conversionRate;
}

Demo here.


Update (JSP for loop):

In the case of your for loop, you can just use the following jQuery code (no need to change the HTML):

$('input[name=amount]').each(function (_, value) {
    $(value).keyup(function () {
        var rate = 1.5;
        var convAmount = $(this).val() * rate;
        $(this).parent().next().children('input[name=convertedAmount]').val(convAmount);
    });
});

See demo here.

Note: Your for loop is generating duplicated IDs. If possible, avoid that as it is invalid HTML.

Sign up to request clarification or add additional context in comments.

5 Comments

this is great! it works! bt is there a way to make it work for textbox in a for loop too? thnks
I saw the for loop at the question now. Do you still need it?
yes I still need the for loop. so is there a way for me to do that? thnks
i tried bt it doesnt seem to work for my for loop...no idea wheres the error... bt i shall try again. Thnks!
i believe this line is for storing the converted value into the convertedAmount textbox rite? $(this).parent().next().children('input[name=convertedAmount]').val(convAmount); However sadly it seems like this line is not working for me.. is there any other way to counter this issue? thnks!
2

Simple using jQuery:

<input type="text" name="t1" id="t1" value="" onkeypress="calculation()" />

<input type="text" name="converted" id="converted" value="" />

<script>
function calculation() {
   var price = 'here is your formula';
   $('#converted').val(price);
}
</script>

2 Comments

Better would be to use onkeyup
@skparwal this works for me as well! bt same as above is there a way to make it work for textbox in a for loop? I tried to use .each(function()) bt i dun think its working. Thnks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.