0

html

<span id="count" style="margin-top: -5%;" class="likes_count">5 likes</span>

this is my javascript code

$(document).ready(function(){
    // when the user clicks on like
    $(document).on('click','.like',function(){
       $('.like').addClass('animated bounceIn');
      var postid = $(this).data('id');
      var id = $(this).attr('data-likes');
          $post = $(this);

      $.ajax({
        url: 'dashboard.php',
        type: 'POST',
        data: {
          'liked': 1,
          'postid': postid
        },
        success: function(response){


          $post.parent().find('span.likes_count').text(id+1+"likes");
          $post.addClass('hide');
          $post.siblings().removeClass('hide');



        }
      });

    });

i am fetching nuber of like in id variable but dont know how to increase it on success ive tried it in my code but not working it is adding 1 in front of the whole number any helps

2
  • Can you add any live demo ? with HTML code ? Commented Oct 20, 2018 at 12:56
  • ...text(parseInt(id)+1+"likes") Commented Oct 20, 2018 at 13:10

1 Answer 1

2

Just replace this:

$post.parent().find('span.likes_count').text(id+1+"likes");

with:

$post.parent().find('span.likes_count').text((+id+1)+"likes");

The problem is that id, as well as "likes" are strings, so Javascript interprets the + operator as doing concatenation between strings. The "unary plus" +id converts it to a number (you can also use Number(id), which some find more explicit but I personally find verbose), and by putting +id+1 in parentheses you ensure that this is treated as numeric addition, before the string concatentation with "likes" happens.

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

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.