On a follow intent event, how do you determine which button was clicked?


For tweet intent events, you can pass data using target.dataset. Is there a similar method for follow intent events?


Yes, you can bind to the follow event, and if a follow occurs somewhere in the flow of the popup web intent, we will pass back this event. If you are using the Follow Button, then the event.target will reference the iframe which generated the follow (even if the follow occurred in an intent popup window). If you are using normal anchors + web intents, then event.target will reference the anchor tag that originally spawned the intent popup.

twttr.events.bind('follow', function(event) { console.log('The follow originated from', event.target); });

Let me know if this answers you question.


I’m using a follow button, with code equivalent to the following.
Follow @ScreenName

Assuming there are multiple follow buttons on a site, is there a way to uniquely identify which twitter-follow-button was clicked? Something equivalent to setting a data- attribute in the iframe?


In the example shown above, the event.target is a reference to the exact iframe (follow button) that was clicked. In your HTML, if you did something like:

Follow @user1
Follow @user2

In the above code snippet, if a follow came from the second follow button, the above would see the following behavior:

event.target = the 2nd iframe that we inject for the follow button
event.target.parentNode = the div we created whose id="user2"
event.data.screen_name = “user2”, the twitter screen_name of the user followed.

Addition information and documentation can be found here: https://dev.twitter.com/docs/intents/events


I’m not sure why I was bent on passing data through the button itself, but this is a clear solution. It’s embarrassingly obvious in hindsight. Thanks for the help.


Hah, no problem at all, glad we could help out!


In our case, I have a few buttons on the page that have ID’s directly on them. Depending on which is clicked, we’d like to have certain functions trigger. Right now, my a tags are swapped out for iframes, and when I get the event back, it doesn’t seem as if I can simply see what a tag (with id) originally generated the iframe.

Am I missing something? Right now, the simplest thing to do seems to be wrapping the a tag and iframe, and then checking the id of the parentnode.


that makes the most sense. since we swap out the < a > for an < iframe >, wrapping the entire thing in a < div > with an id, then checking the parentNode will easily allow you to keep reference on which element the event is being fired on.


It’s possible the know if the user is already following me ? I’m using this example http://jsfiddle.net/imranbohoran/kDqB8/19/
It’s works ok but when i’m already following the account the button grays out and no events are fired. How can i know if the user is already folowwing the account?


Hi @martinib77, did you figure out how to get an event if user is already following the account - meaning the button is grayed out?


wow great, this is work