I'm trying to make facebook share button with javascript.
Here is my code:
<script>
window.fbAsyncInit = function() {
FB.init({
appId: 'my app id',
status: true,
cookie: true,
xfbml: true
});
};
(function(d, debug) {
var js, id = 'facebook-jssdk',
ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement('script');
js.id = id;
js.async = true;
js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ false));
function postToFeed(title, desc, url, image) {
var obj = {
method: 'feed',
link: url,
picture: image,
name: title,
description: desc
};
function callback(response) {}
FB.ui(obj, callback);
}
var fbShareBtn = document.querySelector('.fb_share'); //my problem start
fbShareBtn.addEventListener('click', function(e) {
e.preventDefault();
var title = fbShareBtn.getAttribute('data-title'),
desc = fbShareBtn.getAttribute('data-desc'),
url = fbShareBtn.getAttribute('href'),
image = fbShareBtn.getAttribute('data-image');
postToFeed(title, desc, url, image);
return false;
});
Here is the HTML:
<div id="fb-root"></div>
<a href="mysite.com/mypost" data-image="my image" data-title="my title" data-desc="Let's See and comment" target="_blank" type="button" class="btnku btn-efbe fb_share" id="123">Facebook</a>
The problem is the share button only work for post number 1 or top post. I think it's because the javascript no catch the id
Any answer?