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?

5
  • 1
    Where is a defined? Commented Aug 7, 2016 at 14:29
  • @Vandesh, wherever. For example at the top of script. Commented Aug 7, 2016 at 14:33
  • I can't see any problem in your code. Commented Aug 7, 2016 at 14:33
  • @Mohammad, 39 -1 is 38, and not 39. Commented Aug 7, 2016 at 14:36
  • 1
    @bonaca - Can you include that in the code? What may be happening is it gets evaluated as a string. And Hence 39+1 is getting converted to 391. string + int in Javascript = string Commented Aug 7, 2016 at 14:38

2 Answers 2

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;

Sign up to request clarification or add additional context in comments.

2 Comments

That's it. Have you any link about harmful eval function ?
2

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

4 Comments

but it should not be concatenated !
Yes, but JavaScript takes "+" as string concatenator.
I will. be patient, pls
eval is harmful and not recommended to be used. Here's why - stackoverflow.com/questions/86513/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.