0

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 class="_5n6h _2pih" 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.

7
  • Dumb question, do you have jQuery installed
    – esQmo_
    Commented Nov 6, 2021 at 11:03
  • yes Sir, I have others script that works with it Commented Nov 6, 2021 at 11:06
  • well I guess you need get the value of your span with .text();
    – esQmo_
    Commented Nov 6, 2021 at 11:10
  • I did it too but I have no results. So I was wondering maybe facebook plugins display after the dom is ready ? Commented Nov 6, 2021 at 11:15
  • Just tried and works for me
    – esQmo_
    Commented Nov 6, 2021 at 11:21

1 Answer 1

0

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);
            });
        });
5
  • Hi Sir, thanks for your help. I do have in the console "NaN" so I don't know what behavior is that Commented Nov 6, 2021 at 11:42
  • Doesn't make sense. You might have issues elsewhere
    – esQmo_
    Commented Nov 6, 2021 at 11:44
  • You're now getting a NaN (which is a number, like Infinity) because of the parseInt(). If you call it on something it can't parse, this returns a NaN. So I guess you have an empty value assigned
    – esQmo_
    Commented Nov 6, 2021 at 11:51
  • the fact is that the count box is not empty. It's always superior to 1 or 2 Commented Nov 6, 2021 at 12:00
  • If you remove parseInt, you'll get an empty value. So I'm wondering why your span returns an empty value
    – esQmo_
    Commented Nov 6, 2021 at 12:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.