Hi , how can I increase the quantity of +1 after clicking the button ?


@cssninja83842 I know that you can change html elements based off of this lesson.
Modifying HTML Elements
But "+1" is a string, not a number. You'd have to figure out:
1. Get the value of the button text. like using .val().
2. Get the second character of the string maybe using .charAt(1).
3. Change it from a string to a number value. (Haven't figured this one out and have it work yet.)
4. Add 1 to it.
5. Add that back in to the button text.

Though you can do it every time you click on it it will toggle the Class from being liked to not liked so it would seem weird. And I'm not sure why you'd want to have a button that just counts for you in this situation, though another situation probably it would make more sense.

I've tried to do it but I just haven't been able to figure it out fully.


I figured it out.

  1. A button doesn't have a value so we can't use the .val() like I thought. But it does have text so we can use .text() to retrieve it.
  2. If you use the .parseInt() you don't have to get the character thing I talked about. It will just find the number 1 and make it a number value.
  3. Then you just add it back in with the .html(), such as, $(this).html('+'+(num+1)); Where num is a variable for the number value you got using the parseInt(). Checked and it works. But of course based on the project it toggles the button to liked and not liked as it keeps adding up. But in another situation it might make sense to use this for a button.