javascriptjquerysharethis

Using jQuery to update the st_url attribute of the sharethis chicklet button?


I am dynamically updating the attribute 'st_url' of all of the sharethis spans to the url of a video clicked with jQuery. In firebug you can see it is updating the st_url attribute of my spans, however I the sharethis button is still tied to the initial url. I read I may have to reinit the elements, but I am unsure of the best way to do this? Has anyone done this or have any idea of the best way to re-initialize the buttons with the updated url? Thanks!

Sharethis includes and init:

<script type="text/javascript">
    var switchTo5x=true;
</script>
<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>
<script type="text/javascript">
    stLight.options({publisher:'xxxx'});
</script>

My markup:

<span st_url="http://sharethis.com" class='st_stumbleupon' ></span>
<span st_url="example" class='st_facebook' ></span>
<span st_url="example" class='st_twitter' ></span>
<span st_url="example" class='st_email' ></span>

My jQuery to update the attr="st_url" when a user clicks on a video:

//note: this is triggered by .bind('click')
var youtubeID = $(this).attr('id');
$('#container div > span').each(function(){
    //update sharethis to current video
    $(this).attr('st_url','http://www.youtube.com/watch?v=' + youtubeID);
});

Solution

  • Because there's no updateEntry call in ShareThis you need to use jQuery's .html() to recreate your elements inside your container, then repopulate each one using individual calls to stWidget.addEntry against the span's ID.