1

How to use JQuery to handle the selection of the input do the calculation and display the results.

<input type="text" id="num1"/>

<select name="Calc">
  <option value="add">ADD</option>
  <option value="subtract">SUBTRACT</option>
  <option value="multiply">MULTIPLY</option>
  <option value="divide">DIVIDE</option>
</select>

<input type="text" id="num2"/>

<button id="Calculate">CALCULATE</button>
<button id="clear">CLEAR</button>
Calculated Value:
<div class="valueDisp">
<div id="calc_value">
</div>
</div>

$('input[name=Calc]').ready(function () {
    var selected = $(this).val();
    var num1 = $('#num1').val();
    var num2 = $('#num2').val();
    if (selected == 'add') var total = +num1 + +num2;
    else if (selected == 'subtract') var total = num1 - num2;
    else if (selected == 'multiply') var total = num1 * num2;
    else if (selected == 'divide') var total = num1 / num2;
    else var total = ("Please select an option");
    $("#calculate").click(function () {});
    $("#clear").click(function () {
        $('.input').val("");
    });
    $("#calc_value").html(total);
});

I have the following : http://jsfiddle.net/kHtHA/5/

3 Answers 3

2

You may try this (check the value attribute of select, changed it, but there are other ways)

<option value="add">ADD</option>

become

<option value="+">ADD</option> <!--and so on-->

JS:

$(document).ready(function () {
    $('#Calculate').click(function(){
        var operand = $('select[name=Calc]').val();
        var num1 = $('#num1').val();
        var num2 = $('#num2').val();
        if(num1.match(/\d/) && num2.match(/\d/)) {
            var total = eval(num1 + operand + num2);
            $("#calc_value").html(total);
        }
    });
});

DEMO.

Sign up to request clarification or add additional context in comments.

Comments

1

Check the update : http://jsfiddle.net/kHtHA/7/

    var doCalculate = function () {
        var selected = $('select[name="Calc"]:eq(0)').val();
        var num1 = $('#num1').val();
        var num2 = $('#num2').val();
        if (selected == 'add') var total = +num1 + +num2;
        else if (selected == 'subtract') var total = num1 - num2;
        else if (selected == 'multiply') var total = num1 * num2;
        else if (selected == 'divide') var total = num1 / num2;
        else var total = ("Please select an option");
        $("#calc_value").html(total);
    };
    $('#Calculate').click(doCalculate);
    $('select[name="Calc"]').change(doCalculate);

Comments

1

Not 100% sure what you want. A fiddle update: http://jsfiddle.net/kHtHA/14/.

$('#Calculate').on('click', function(e){
    var num1 = parseInt($('#num1').val());
    var num2 = parseInt($('#num2').val());

    if(isNaN(num1) || isNaN(num2)){
        return;
    }

    var operator = $('select[name=Calc]').val();
    var total;
    switch(operator){
        case "add":
            total = num1 + num2;
            break;
        case "subtract":
            total = num1 - num2;
            break;
        case "multiply":
            total = num1 * num2;
        break;
        case "divide":
            total = num1 / num2;
            break;
    }

    $('#calc_value').html(total);
});

$('#clear').on('click', function(e){
    $('#num1').val("");
    $('#num2').val("");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.