bonaca bonaca - 4 months ago 12
HTML Question

js variable and radio button checked / unchecked

<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?

Answer

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;