[Solved] Can't get plus minus button (+/-) to work on js calculator

[Solved] Can't get plus minus button (+/-) to work on js calculator
0

#1

When someone clicks the +/- button on my calculator, it should add a negative sign if the number is positive, and take away the negative sign if the number is negative.

It seems like a simple task to write a function for this, but it’s not working. I’m using jQuery.

I’ll just post the relevant parts of my code.

Under HTML, I have:

<input type="text" name="output" id="output">
<input type="button" name="opp" value="+/-" id="opp" onclick="opposite()"/>

Under JS, I have:

$("#opp").click(function(opposite){
   var b=document.getElementById("output");
   var n=b.value; 
   n = n * -1;
});

How can I get this working?

Thanks!


#2
$("#opp").click(function(opposite){
   var b=document.getElementById("output");
   var n=b.value; 
   n = n * -1;
   b.value = n; // assign the value of n back to the output
});

#3

Oh my gosh! I knew it had to be so simple! lol

That worked!

Thank you!