Home > Enterprise >  Using jQuery to have facebook like count
Using jQuery to have facebook like count

Time:11-06

Since opengraph only provide those informations

array(2) { ["engagement"]=> array(4) { 
["reaction_count"]=> int(1) 
["comment_count"]=> int(0) 
["share_count"]=> int(1) 
["comment_plugin_count"]=> int(0)  
}

I'm trying to have the like count of a url.

So I found this information on the button like, which have this number.

<span class="_49vh _2pi7">J’aime</span><span  id="u_0_2_/ ">2</span>

I found that the span with the count has an Id but it change for each page. But the class used is the same for each pages ._5n6h ._2pih

I'm trying to have the count of this span.

My script was very simple

  <script type="text/javascript" async defer>
                                            $(document).ready(function () {
                                                var elmId = $('._5n6h._2pih').html();
                                                console.log(elmId);
                                            })
                                        </script>

I can not have them

Uncaught TypeError: $(...).html is not a function

I don't know what am I doing wrong.

CodePudding user response:

You first need to include jQuery at the top, preferably in the <head> then try the following:

    $(document).ready(function(){
   var count = $('span._5n6h._2pih');
   var like_count = parseInt(count.text());

   console.log(like_count); //outputs 2



    //I have played a bit with your code adding a click listener to the span. Each time you click, it increments by 1
        like.click(function(){
like_count  = 1;
count.text(like_count);
            });
        });
  • Related