Reputation: 898
I have the 3 HTML inputs, 2 which receive user input and one which displays a calculation once a button named #calculate is pressed. The following piece of code will correctly perform the calculation upon the clicking of the #calculate button, but I am looking for the most efficient way to have the form submit the form when the user enters the ENTER/RETURN key in addition of clicking on the button with mouse.
$('#calculate').click(function(){
var count = 0;
count += checkRequired('#field_3_a');
count += checkRequired('#field_3_b');
if (count == 0)//enough data
{
//calculate
var a = parseFloat($('#field_3_a').val());
var b = parseFloat($('#field_3_b').val());
var totalValue = (b - a) / a * 100;
$('#result_3').val(totalValue);
}
});
Your input is appreciated.
Upvotes: 0
Views: 35
Reputation: 27811
You can handle the keypress
event on the text fields, or the form, and check if the key pressed was enter
:
$('#field_3_a, #field_3_b').keypress(function(event) {
if (event.which === 13) {
event.preventDefault();
//do something, like call calculate()
}
});
Upvotes: 1