<h2>Calculadora</h2> <input type="text" class="form-control" id="num1" value="4"> <input class="btn btn-info" type="button" id="sumar" value="+"> <input class="btn btn-info" type="button" id="restar" value="-"> <input class="btn btn-info" type="button" id="multiplicar" value="*"> <input class="btn btn-info" type="button" id="dividir" value="/"> <input type="text" class="form-control" id="num2" value="5"> <h3>=</h3> <input type="text" class="form-control" id="res">
$(function () { $("#sumar").click(function () { var n1 = Number($('#num1').val()); var n2 = Number($('#num2').val()); $('#res').val(n1 + n2); }); $("#restar").click(function () { var n1 = Number($('#num1').val()); var n2 = Number($('#num2').val()); $('#res').val(n1 - n2); }); $("#multiplicar").click(function () { var n1 = Number($('#num1').val()); var n2 = Number($('#num2').val()); $('#res').val(n1 * n2); }); $("#dividir").click(function () { var n1 = Number($('#num1').val()); var n2 = Number($('#num2').val()); $('#res').val(n1 / n2); }); });