Follow Button JavaScript Factory Function

The Twitter for Websites JavaScript library supports dynamic insertion of a follow button using the twttr.widgets.createFollowButton function. Pass Twitter username, target parent element, and any custom options.

The code snippets on this page assume widgets.js has successfully loaded on your page. Include an asynchronous script loader on your page while initializing window.twttr as described in our JavaScript loader documentation. All JavaScript code depending on widgets.js should execute on or after twttr.ready.


Parameter Description Example value
username A Twitter username 'TwitterDev'
targetEl DOM node of the desired parent element document.getElementById('container')
options Override default widget options { size: 'large' }


An element with a DOM ID of container exists on the page.

      <div id="container"></div>

The code snippet below will create a new Follow button for the Twitter username “TwitterDev” inserted into a page inside an element with a unique ID of container.

    size: 'large'


The twttr.widgets.createFollowButton function returns a Promise. You can execute code after a widget has been inserted onto your page by passing a callback to the resulting promise’s then function.

.then( function( el ) {
  console.log('Follow button added.');
Was this document helpful?
Thank you

Thank you for the feedback. We’re really glad we could help!

Thank you for the feedback. How could we improve this document?
Thank you for the feedback. Your comments will help us improve our documents in the future.