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
  • 9,025
  • 18
  • 73
  • 126

2 Answers2

2

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

Ravi Shankar Bharti
  • 8,922
  • 5
  • 28
  • 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
  • 6,368
  • 1
  • 26
  • 38