jQuery Sum & Subtract Two Input Fields
jQuery Sum & Subtract Two Input Fields
HTML
<form name="form1" method="post" action="" >
<table>
<tr><td>X*Y=</td><td><input type="text" name="num1" id="num11" /></td></tr>
<tr><td>X+Y=</td><td><input type="text" name="num2" id="num22" /></td></tr>
<tr><td>X=</td><td><input type="text" name="xres" id="xres" readonly /></td></tr>
<tr><td>Y=</td><td><input type="text" name="yres" id="yres" readonly /></td></tr>
</table>
</form>
JavaScript
$(document).ready(function() {
//this calculates values automatically
func();
$("#num1, #num2").on("keydown keyup", function() {
func();
});
});
function func() {
var num1 = document.getElementById('num1').value;
var num2 = document.getElementById('num2').value;
var result = -2 * parseInt(num1) / (-1 * parseInt(num2) + Math.sqrt(parseInt(num2) * parseInt(num2) - 4 * parseInt(num1)));
var result1 = -2 * parseInt(num1) / (-1 * parseInt(num2) - Math.sqrt(parseInt(num2) * parseInt(num2) - 4 * parseInt(num1)));
if (!isNaN(result)) {
document.getElementById('xres').value = result;
document.getElementById('yres').value = result1;
}
}