Element represents one mounted card field (combined card, or one of the split types). Get one from elements.create(type, options?).
Methods
ElementType
required
The Element type this instance renders.
mount(target)
Insert the Element’s iframe into a container on the page.
Requires HTTPS outside localhost. Calling mount() on an already-mounted Element logs a warning and does nothing. The Element destroys itself when its container is removed from the DOM and when the page unloads. Emits ready once the iframe has loaded; if it has not loaded within 5 seconds a change event with error.code === 'required' is emitted.
string | HTMLElement
required
A CSS selector or the container element.
element:destroyed(load_error) — the Element was destroyed.element:container_not_found(load_error) — no element matchestarget.
unmount()
Remove the iframe from the page. The Element keeps its event handlers and can be mounted again. Does nothing if not mounted.
destroy()
Unmount and permanently release the Element: removes all event handlers and frees its type so Elements.create can create it again. Idempotent.
on(event, handler)
Subscribe to an Element event.
K
required
Event name. See
ElementEventMap for payloads.(e: ElementEventMap[K]) => void
required
Called with the event payload.
off(event, handler)
Remove a handler previously added with Element.on.
K
required
Event name.
(e: ElementEventMap[K]) => void
required
The same function reference passed to
on().focus()
Move focus into the field. Does nothing if not mounted.
blur()
Remove focus from the field. Does nothing if not mounted.
clear()
Clear the field’s input and emit change. On the combined 'card' Element only the card number is cleared.
update(options)
Restyle this Element only. To restyle every Element, use Elements.update.
The appearance is sanitized with the same rules as AppearanceOptions. Safe to call before the iframe is ready — the latest value is applied on ready.
{ appearance?: AppearanceOptions; }
required
appearance to apply. Calls without appearance are ignored.getState()
The field state as of the most recent change event.
Synchronous and cached. For a live read of every field, use Elements.getState.
Returns FieldState — A copy of the cached FieldState.
Events
ReadyEvent
Payload of the
ready event — emitted once the iframe has loaded and the field accepts input.Emitted again after each re-mount().ChangeEvent
Payload of the
change event — emitted when a field’s value, completion or validity changes.Fired on input, paste, deletion and clear(). Also fired with error.code === 'required' if the iframe fails to load within 5 seconds (for example when a browser extension blocks it), and once more with error: null if it loads later.complete vs valid. For every Element type the two flags are currently always equal: a field only becomes complete once its content passes validation. - Card number: a valid length for the detected brand and a passing Luhn checksum, and — when CreateElementOptions.supportedBrands is set — a recognised brand must be in that list. - Expiry: four digits forming a month 01–12, not in the past, and at most 12 years ahead. - CVV: the full length for the detected brand (4 for Amex, 3 otherwise). - Combined 'card': every visible cell is complete and valid.Gate your pay button on valid.error is produced by blur-time validation, so it can be null while valid is false (the shopper is still typing). Use error for messaging, not for gating.FocusEvent
Payload of the
focus event — emitted when the field receives focus.BlurEvent
Payload of the
blur event — emitted when the field loses focus.CardTypeChangeEvent
Payload of the
cardTypeChange event — emitted when the detected card brand changes as the shopper types the card number.Emitted by 'card' and 'cardNumber' Elements only, and only when the brand actually changes (not on every keystroke).Element events
FieldErrorCode values
Example
- Vanilla JS
- React