Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

b.createDocumentFragment is not a function (jQuery)

I'm playing around with a function and getting

b.createDocumentFragment is not a function (jQuery)

My function is

function tweetCount(url) {  
    $.getJSON("http://urls.api.twitter.com/1/urls/count.json?url="+url+"&callback=?", function(data) {
      count = data.count
      $(this).append(count);
   })
}

I've tried lots of different way but can't seem to find out why it doesn't like "append". "count" is a number and something like alert(count) works, but not append!

Any help?! Alex

like image 567
Alex Fox Avatar asked Aug 10 '11 18:08

Alex Fox


1 Answers

I don't think that this is referring to what you think it is. Change $(this) to an explicit reference to the DOM element you want.

Alternatively, you can define this by calling:

tweetCount.call($("#element"), url)

Edit

Try this:

$("span.tweetcount").each(function(){
    url = $(this).attr('title');
    tweetCount.call(this, url);
});

Or, to save space:

$("span.tweetcount").each(function(){
    tweetCount.call(this, $(this).attr('title'));
});

Edit 2:

Try replacing tweetCount with this:

function tweetCount(url) {  
    var that = this;
    $.getJSON("http://urls.api.twitter.com/1/urls/count.json?url="+url+"&callback=?", function(data) {
        count = data.count;
        $(that).append(count);
})
like image 154
Chris Laplante Avatar answered Sep 28 '22 18:09

Chris Laplante