> ## Documentation Index
> Fetch the complete documentation index at: https://x-preview-mintlify-f9d5aa0e.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Post Button JavaScript Factory Function

> Dynamically insert a Post button with twttr.widgets.createShareButton by passing a share URL, target element, and options like pre-populated share text.

The X for Websites JavaScript library supports dynamic insertion of a Post button using the `twttr.widgets.createShareButton` function. Pass a share URL, 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](/x-for-websites/javascript-api/guides/set-up-x-for-websites). All JavaScript code depending on `widgets.js` should execute on or after `twttr.ready`.

## Arguments

| Parameter  | Description                                                                                                                                | Example value                          |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------- |
| `url`      | A share URL, or an empty string if you would not like to include a Post composer URL component                                             | `https://dev.x.com/`                   |
| `targetEl` | DOM node of the desired parent element                                                                                                     | `document.getElementById('container')` |
| `options`  | Override default widget options. See [Post button parameter reference](/x-for-websites/post-button/guides/parameter-reference) for details | `{ text: 'Hello world' }`              |

## Example

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

```html theme={null}
<div id="container"></div>
```

The code snippet below will create a new Post button with pre-selected share text of “Hello world” and a shared URL of “/” inserted into a page inside an element with a unique ID of `container`.

```javascript theme={null}
twttr.widgets.createShareButton(
  '/',
  document.getElementById('container'),
  {
    text: 'Hello World'
  }
);
```

## Promises

The `twttr.widgets.createShareButton` function returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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.

```javascript theme={null}
twttr.widgets.createShareButton(...)
.then( function( el ) {
  console.log('Post button added.');
});
```

## Hashtag, mention buttons

Create a hashtag or mention button using the [twttr.widgets.createHashtagButton](/x-for-websites/post-button/guides/hashtag-button) and [twttr.widgets.createMentionButton](/x-for-websites/post-button/guides/mention-button) functions respectively. The same object applies.
