Driving me nuts


#1

Hi,

pressing +1 just adds an 1 to the text. 10 becomes 101
pressing -1 neatly subtracts 1. 10 becomes 9

what is the mistake?

tia,

John

plusmin.hype.zip (12.0 KB)


(Greg) #2

Welcome to javascript :flushed: – try this, it works…

var x = hypeDocument.getElementById("qty").innerHTML;

    x = x * 1 + 1; // the plus sign is used for concatenation
    
hypeDocument.getElementById("qty").innerHTML = x;

(Mark Hunte) #3

I think the problem is that you are working in effect with strings. so the plus symbol is being used to concatanate the two strings.

There are a couple of ways to force the string into numbers but the simplest in this case is to just do:

var x = hypeDocument.getElementById("qty").innerHTML;
  
  x++;
    
    hypeDocument.getElementById("qty").innerHTML = x;

If you wanted to do something like:

var x = hypeDocument.getElementById("qty").innerHTML;
  
     x = x + 400;
        
       hypeDocument.getElementById("qty").innerHTML = x;

Then you would need to cast x into a number. One way of doing it is :

var x = hypeDocument.getElementById("qty").innerHTML;
  
 x = Number(x) + 400;
    
  hypeDocument.getElementById("qty").innerHTML = x;

#4

thank you @gasspence and @MarkHunte. You just saved my weekend!
I really need to dive deeper into JS.