// gets the facebook comments count for all elements with class 'ut-fb-comments-count' $(".ut-fb-comments-count").each(function(){ $.ajax({ context: this, url: 'https://graph.facebook.com/' + this.getAttribute('data-href'), dataType: 'jsonp', success: function(data) { if(typeof data.share !== 'undefined'){ if(data.share.comment_count > 0){ if(this.parentNode.parentNode.tagName == 'LI'){ // remove the class 'last' for li's $(this.parentNode.parentNode).prev().removeClass('last'); }else if(this.parentNode.parentNode.tagName == 'H2'){ // add pipe before comment count this.parentNode.parentNode.insertBefore(document.createTextNode(" | "), this.parentNode); } this.innerHTML = data.share.comment_count + " Comment"; if(data.share.comment_count > 1){ this.innerHTML = this.innerHTML + 's'; } } } } }); });