<div class="container"> <h2>Calculadora <b>poco científica</b></h2> <input type="text" class="form-control" id="num1" value="4"> <input class="btn btn-info boton" type="button" id="suma" value="+"> <input class="btn btn-info boton" type="button" id="resta" value="-"> <input class="btn btn-info boton" type="button" id="multi" value="*"> <input class="btn btn-info boton" type="button" id="divi" value="/"> <input class="btn btn-info" type="button" id="reset" value="Borrar campos"> <input type="text" class="form-control" id="num2" value="5"> <h3>=</h3> <input type="text" class="form-control" id="res"> </div>
$(function () { $(".boton").click(function () { var n1 = Number($('#num1').val()); var n2 = Number($('#num2').val()); var operacion = $(this).val(); $('#res').val(eval(n1 + operacion + n2)); $('#res').after("<p>"+n1 + " " + operacion + " " + n2 + " = " + $('#res').val()+"</p>"); }); $("#reset").click(function () { $(':text').val(""); }); });