Search code examples
phprefreshsocial-media-like

Refreshing a specific div id among many divs onclick href having that same id


I want to display like count for that post the user likes, every post like href has a specific id, now if I am clicking the post having id 222. Then like count having id 222 will refresh.

<a href="" onclick="rate('111');">like</a> <div id="111">4</div>
<a href="" onclick="rate('222');">like</a> <div id="222">5</div>
<a href="" onclick="rate('333');">like</a> <div id="333">12</div>

My rate function

<script type="text/javascript">
function rate(c) 
{   
        var c   
    jQuery.ajax({
        type: 'POST',
        url: 'rate.php',
        data: {
                cmnt: c     
               }
         });  
          return false; 
}
</script>

the C variable is getting the post id


Solution

  • you can update your rate() function as given below

    <script type="text/javascript">
        function rate(c) 
        {   
            jQuery.ajax({
                type: 'POST',
                url: 'rate.php',
                data: {cmnt: c},
                success: function(html) {
                    var count = $("#"+c).html();
                    var new_count = parseInt(count)+parseInt(1)
                    $("#"+c).html(new_count);
                 }
                 });  
                  return false; 
        }
        </script>
    

    let me know if that helped you...