Embedded Moment JavaScript Factory Function

The Twitter for Websites JavaScript library supports dynamic insertion of a Moment using the twttr.widgets.createMoment function. Pass a Moment ID, target container element, and an optional options object to insert an embedded Moment into your page.

The code snippets on this page assume Twitter’s 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.

Arguments

Parameter Description Example value
moment_id The Moment identifier '650667182356082688'
targetEl DOM node of the desired parent element document.getElementById('container')
options Override default widget options { limit: 3 }

Example

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

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

The code snippet below will create a embedded Moment for Moment ID “650667182356082688” inserted into a page inside an element with a unique ID of container.

twttr.widgets.createMoment(
  '650667182356082688',
  document.getElementById('container'),
  {
    limit: 3
  }
);

Promises

The twttr.widgets.createMoment 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.

twttr.widgets.createMoment(...)
.then( function( el ) {
  console.log('Moment added.');
});