1

When somebody is liking a comment on my website, a "1" is added at the right of the number where the amount of likes are shown, but when they click dislike, it does correct math.

For example:

14 + 1 = 141
14 - 1 = 13

jQuery

var elem   = $('.like_button'), //Like button
    num    = $('.num_likes'), //Get the element: number of likes
    oldnum = num.html(); //Number of likes

if(elem.html() == "Like") {
    elem.html("Dislike");
    num.html(oldnum+1); //Adds one like after liking it
} else {
    elem.html("Like");
    num.html(oldnum-1); //Deletes one like after disliking it
}

I really wonder why disliking works but liking not.

Why does javascript interpret the value of the num element as a string, even though it is a number? Any tips for me?

4
  • You could use: var oldnum = + $.trim(num.html()); Commented May 2, 2014 at 19:57
  • - is only a numeric operator (you can't subtract strings), so javascript coerces oldnum to be a number. + works with both numbers and strings, so javascript coerces the second operand (the 1) to a string, since that matched oldnum. Commented May 2, 2014 at 19:57
  • var oldnum = num.html(); //Number of likes is a string Commented May 2, 2014 at 19:57
  • you've got to convert oldnum to integer. parseInt(oldnum,10) should do the trick Commented May 2, 2014 at 19:57

3 Answers 3

3

Because JavaScript interprets num.html() as text. The + sign for string in javascript means concatenation, but - doesn't mean that so in that case javascript realizes you want to do numeric calculation. That's why it works with -

You should cast oldnum to an integer with parseInt().

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

6 Comments

As a (sad) fact, yeah: in javascript the string concatenation and the addition operators are both expressed with '+'. That's not the case in many other languages, such as php,lua..
And you can parse oldnum into a number like so: oldnum = parseInt(num.html(), 10);
Why the "10" in parseInt? What does it stand for?
That's radix. From w3 schools: If the radix parameter is omitted, JavaScript assumes the following: If the string begins with "0x", the radix is 16 (hexadecimal) If the string begins with "0", the radix is 8 (octal). This feature is deprecated If the string begins with any other value, the radix is 10 (decimal)
Depends what you want. Number will say NaN in case if you try Number('10px'), but parseInt will return 10. I would say Number is more strict...
|
1

You need to cast oldnum to a number:

if(elem.html() == "Like") {
    elem.html("Dislike");
    num.html(Number(oldnum)+1); //Adds one like after liking it
} else {
    elem.html("Like");
    num.html(Number(oldnum)-1); //Deletes one like after disliking it
}

Alternatively, +oldnum does the same thing as Number(oldnum).

2 Comments

What do you mean with +oldnum? num.html(+oldnum) ?
Instead of num.html(Number(oldnum)+1), you can use num.html((+oldnum)+1).
1

Javascript is interpreting the text on your page as a string. This is because that's what text on a page normally is. Take for example:

<span id="berliner">I am a jelly donut.</span>
<script LANGUAGE="Javascript">
document.getElementById("berliner").innerHTML;
// it only makes sense that this be a string, right?
</script>

Now, in JS, you use the + sign for two things: adding numbers, or putting one string after another.

var addingnumbers = 1+1;
// adding numbers, what you want
var a = "I am";
var b = " a jelly donut";
var addingstrings = a+b;
// adding strings, which you don't want.

As such, the html was interpreted as a string like it normally should be, but in this case shouldn't be. And adding the string to the other string just appended it to the end, rather than doing math. There is an easy solution: convert the innerHTML to a number by multiplying it by 1. Multiplying can't be done to a string, so JS will change it to number form, prepping it to be added to something else.

var oldnum = num.html()*1; // done! The multiplying has changed it to a number.

And if you ever do want to change it back to a string, you can do the reverse with the toString() function.

var aNumberToStartOutWith = 3;
var aStringToEndOffWith = aNumberToStartOutWith.toString();

Comments

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.