0

i'm working on computing the product of two textboxes. However, the user needs to press "enter" before the txtTotal will show the total value. Pls I really need help. I'm not really familiar with jquery, and I'm still studying jscript. Thank you so much !

Here is my updated code:

<html>
<head>
<script src="jquery-1.11.2.min.js"></script>
<script>

$('#txtQuantity').bind('input',function(){ 
 var quantity = parseFloat($(this).val()) || 0;
 var amount = parseFloat($('#txtAmount').val());
 var evt = (amount * quantity);
 $('#txttotal').val(evt);
});

</script>
</head>

<body>

<input type="text" class="quan1" name="txtQuantity" id="txtQuantity"/> 
<input type="text"  id="txtAmount" class="quan1" name="txtAmount"   value="4" disabled="" /> 
<input type="text" class="quan1" id="txttotal" name="txttotal"  disabled="" /> <br>

</body>
</html>

2 Answers 2

1
var e = $.Event( "keypress", { which: 13 } );
$('#yourInput').trigger(e);

hope this helps

Extra : Read Events and Triggers

if you are using jquery you can do like this.

Solution 1

$('#txtQuantity').on('keypress', function (event) {
       if(event.which === 13){

     var quantity = parseFloat($(this).val()) || 0;
     var amount = parseFloat($('#txtAmount').val());
     var evt = (amount * quantity);
     $('#txttotal').val(evt);
     }
 });

Solution 1 Demo

Solution 2

$('#txtQuantity').bind('input',function(){ 
     var quantity = parseFloat($(this).val()) || 0;
     var amount = parseFloat($('#txtAmount').val());
     var evt = (amount * quantity);
     $('#txttotal').val(evt);
});

Solution 2 Demo

Note: In HTML5 there is a new event, "input", which behaves exactly like you seem to think "change" should have behaved - in that it fires as soon as a key is pressed to enter information into a form. older browsers you can use the "Keyup" event.

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

15 Comments

Hi! Thank you for responding immediately ! I really appreciate it ! I tried your code like this ==> <script src="jquery-1.11.2.min.js"></script> <script> $('#txtQuantity').on('keypress', function (event) { if(event.which === 13){ var quantity = parseFloat($(this).val()) || 0; var amount = parseFloat($('#txtAmount').val()); var evt = (amount * quantity); $('#txttotal').val(evt); } }); </script> .. however it did not work and i'm not so sure if the problem is how i called the function or i'm not really sure. :( Please help me analyze it.
i also tried this ==> <script src="jquery-1.11.2.min.js"></script> <script> var e = $.Event( "onchange", { which: 13 } ); $('#txtQuantity').trigger(e); function jsComputeTotal(evt){ var quantity = parseFloat(document.js.txtQuantity.value) || 0; var amount = parseFloat(document.js.txtAmount.value); var evt = parseFloat(document.js.txttotal.value); evt = (amount * quantity); document.getElementById("txttotal").value = evt ; return false; } </script> and adding onchange="return jsComputeTotal(event)" to txtQuantity but it did not work :(
i saw your Demo, and it is also the same as my code above sir... I would really like to compute without pressing enter?
what is the event you want to trigger.
if you want to trigger the event without pressing enter use change event.
|
0

If you need to compute the value immediatly whithout the user pressing enter, bind a keyup event to the text box

var compute = function(){
     var quantity = parseFloat($("#txtQuantity" ).val()) || 0;
     var amount = parseFloat($('#txtAmount').val());
     var total = (amount * quantity);
     $('#txttotal').val(total);
}

 $( "#txtQuantity" ).keyup(compute);

2 Comments

Hi! Thank you for responding so fast.. I tried your code sir .. i just put this in the script var compute = function (){ var quantity = parseFloat(document.js.txtQuantity.value) || 0; var amount = parseFloat(document.js.txtAmount.value); var compute = parseFloat(document.js.txttotal.value); compute = (amount * quantity); } $("#txtQuantity").change(compute); $("#txtAmount").change(compute);.. but then it is not working. Please help me analyze your code maybe i did not understand your instruction.. Thank you. Please bare with me.
The solution wasnt working because the change event was being trigged after the element loosed its focus. The new solution shoud work as expected.

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.