1
<input id='rb1' type="radio" name="bafter" checked>
<input id='rb2' type="radio" name="bafter">

js

$(".item").click(function(){
    console.log(a); // 39 (that's ok)
    if ($('#rb1').prop('checked') == true) {a -=1;}
    else {a +=1;}
});

console.log(a);

If rb1 checked result is - 38 (ok)
If rb2 is not checked reusult is - 391 (should be 40)

Any help?

qadenza
  • 8,611
  • 18
  • 61
  • 108

2 Answers2

2

It is concatenating as a string. Try a =eval("a+1")

Ravi Shankar Bharti
  • 8,269
  • 4
  • 23
  • 52
2

a might be getting evaluated as a string.
And string + int = string in Javascript
So, "39" + 1 = "391"
Use a = parseInt(a) + 1; and a = parseInt(a) - 1;

Vandesh
  • 5,508
  • 1
  • 24
  • 31