I'm developing a calculator in HTML + JQuery where I have basic operations for it, however I need to implement the following:
When the user types in the input field an expression type: (5 * 5) +4 JQuery the operation and show the result. any idea how you could be doing this?
$("input[name=exp]").click(function(){
valor = $("#result").val();
$("#result").val(valor);
});
follows my JQuery code:
$( function(){
var v1, v2, op;
$("input[name=btn]").click(function(){
$("#result").val($("#result").val() + $(this).val());
});
$("input[name=ce]").click(function(){
$("#result").val('');
$("#op").text('');
});
$("input[name=soma]").click(function(){
if($("#result").val() != ''){
v1 = parseFloat($("#result").val());
$("#result").val('');
op = "soma";
$("#op").text($(this).val());
}else{
alert("Insira um valor para efetuar a operação");
}
});
$("input[name=menos]").click(function(){
if($("#result").val() != ''){
v1 = parseFloat($("#result").val());
$("#result").val('');
op = "menos";
$("#op").text($(this).val());
}else{
alert("Insira um valor para efetuar a operação");
}
});
$("input[name=vezes]").click(function(){
if($("#result").val() != ''){
v1 = parseFloat($("#result").val());
$("#result").val('');
op = "vezes";
$("#op").text($(this).val());
}else{
alert("Insira um valor para efetuar a operação");
}
});
$("input[name=divisao]").click(function(){
if($("#result").val() != ''){
v1 = parseFloat($("#result").val());
$("#result").val('');
op = "divisao";
$("#op").text($(this).val());
}else{
alert("Insira um valor para efetuar a operação");
}
});
$("input[name=igual]").click(function(){
if($("#result").val() != ''){
v2 = parseFloat($("#result").val());
if(op == "soma"){
$("#result").val(v1 + v2);
}else if(op == "menos"){
$("#result").val(v1 - v2);
}else if(op == "vezes"){
$("#result").val(v1 * v2);
}else{
$("#result").val(v1 / v2);
}
}else{
alert("Insira um valor para efetuar a operação");
}
});
$("input[name=apagar").click(function(){
var len = $("#result").val().length;
var valor = $("#result").val();
valor = valor.replace(valor.charAt(len - 1), "");
$("#result").val(valor);
});
});