4 thoughts on “jQuery Sum & Subtract Two Input Fields”
Hi, I think the function sum() can be rewritten as follows:
function sum(){
$(“#sum”).val(Number($(“#num1”).val())+Number($(“#num2”).val()));
$(“#subt”).val(Number($(“#num1”).val())-Number($(“#num2”).val()));
}
Also, $(“#num1, #num2”).on(“keydown keyup”, function() {
sum();
});
can be rewritten as $(“#num1, #num2”).on(“keydown keyup”,sum);
Hi, I think the function sum() can be rewritten as follows:
function sum(){
$(“#sum”).val(Number($(“#num1”).val())+Number($(“#num2”).val()));
$(“#subt”).val(Number($(“#num1”).val())-Number($(“#num2”).val()));
}
Also, $(“#num1, #num2”).on(“keydown keyup”, function() {
sum();
});
can be rewritten as $(“#num1, #num2”).on(“keydown keyup”,sum);
LikeLike
Thanks for your input Deepak, i would appreciate if you write your code in Code block.
For example like this:
$(document).ready(function() { //this calculates values automatically sum(); $("#num1, #num2").on("keydown keyup",sum); }); function sum(){ $("#sum").val(Number($("#num1").val())+Number($("#num2").val())); $("#subt").val(Number($("#num1").val())-Number($("#num2").val())); } }For your convenience you can check code block format here
https://en.support.wordpress.com/markdown-quick-reference/
Or leave a jsfiddle.net working demo link, as i provided above in my tutorial. 🙂
LikeLiked by 1 person
You can find the fiddle for the same: http://jsfiddle.net/emaildeepak/uzneyvar/
LikeLiked by 1 person
Great Deepak, nice share 🙂
LikeLike