0

I would like to know how to add support for multiplication in this jQuery solution: jquery calculation question

It works well for addition and substraction, but how can it support multiplication?

thanks.

You can see the code example here: http://jsfiddle.net/JRcqk/1/

Community
  • 1
  • 1
Toni
  • 1
  • 1
  • 1
    Having a good laugh at the expense of other developers, aren't you? – Anurag Jul 13 '10 at 18:51
  • Nope. that was a valid question. I am new to jQuery so I am the one people should laugh at. – Toni Jul 13 '10 at 18:53
  • 1
    Fair enough. In that case, see what the developers did on the other question and try to apply it to multiplication **yourselves**, and if you get stuck anywhere, then post your problem. Just saying, that's a better way to learn. – Anurag Jul 13 '10 at 18:56

2 Answers2

0

You would need some changes in the logic, and also use the dreaded eval .. (if you want an agnostic way of handling the calculations)

$('#Selection').change(calc); //update result when changing the number
$("#number").keyup(calc);     //update result when select changes

function calc() {
  $('#result').val( eval( $('#number').val() + $("#Selection").val() ) );
}

and HTML

<div>
    <input id="number" type="text" />

    <select id="Selection" name="D1"> 
    <option value="+1">Plus 1</option>
    <option value="-1">Minus 1</option>
    <option value="*2">double</option>
    <option value="/2">half</option>

    </select>

    <input id="result" type="text" />
</div>
Community
  • 1
  • 1
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
0

You can try to avoid the eval function if you use a switch statement and go throguh the values. check this: switch statement in Jquery and List

Also the solution that Meder proposed was good.

@Gaby - your solution will not perform the addition properly. it will just concatenate both numbers unless you use the parseInt().

@Anurag - It is in the interest of everyone to keep place positive. If you don not wish to contribute to a question then keep going. don't just stop and hurl comments that do not contribute to the question. That does not do any good to the community.

Community
  • 1
  • 1
Lopez
  • 1