Skip to main content

Element Methods

Once you have created and mounted an element, you can call these methods to interact with it. All element types share the same interface.


Properties

PropertyTypeDescription
idstringUnique identifier assigned at creation time
type'cardNumber' | 'cvv' | 'expiry' | 'text'The element type
mountedbooleantrue when the element is attached to the DOM

Methods

mount(selector)

Attaches the element iframe to the DOM. Must be awaited — the promise resolves when the element is loaded and the ready event has fired.

mount(selector: string | HTMLElement): Promise<void>
await cardNumberEl.mount('#card-number-container');
// element is now interactive

Pass either a CSS selector string or a direct HTMLElement reference.


unmount()

Removes the element iframe from the DOM and cleans up all event listeners. Synchronous.

unmount(): void
cardNumberEl.unmount();

update(options)

Sends new options to the element iframe. Returns a promise that resolves when the element has applied the update.

update(options: Partial<ElementOptions> & { themeMode?: 'light' | 'dark' | 'auto' }): Promise<void>
await cardNumberEl.update({
placeholder: 'Enter card number',
disabled: false,
});

You can also update the theme mode for a single element without affecting others:

await cardNumberEl.update({ themeMode: 'dark' });

See Element Options for the full list of options that can be passed to update().


focus()

Programmatically focuses the input inside the element iframe. Synchronous.

focus(): void
cardNumberEl.focus();

blur()

Programmatically blurs (removes focus from) the input. Synchronous.

blur(): void
cardNumberEl.blur();

clear()

Clears the current input value. Synchronous.

clear(): void
cardNumberEl.clear();

on(eventType, listener)

Subscribes to an element event. Returns an unsubscribe function — call it to remove the listener.

on(eventType: 'ready' | 'change' | 'focus' | 'blur' | 'error', listener: (event: CustomEvent) => void): () => void
const unsubscribe = cardNumberEl.on('change', (event) => {
console.log(event.detail.isValid, event.detail.cardBrand);
});

// Later, when the listener is no longer needed:
unsubscribe();

See Events for the full payload reference for each event type.


React refs

In React, element methods are accessible via a ref. The ref type extends the base Element interface with three convenience state properties:

PropertyTypeDescription
completebooleantrue when the input is valid and complete
emptybooleantrue when the input is empty
validbooleantrue when the current value passes validation

The CardNumberElementRef also exposes cardBrand?, last4?, and bin?.

import { useRef } from 'react';
import { CardNumberElement, type CardNumberElementRef } from '@basis-theory/react-elements';

function PaymentForm() {
const cardNumberRef = useRef<CardNumberElementRef>(null);

const handleReset = () => {
cardNumberRef.current?.clear();
};

const handleFocus = () => {
cardNumberRef.current?.focus();
};

return (
<>
<CardNumberElement id="card-number" ref={cardNumberRef} />
<button onClick={handleReset}>Clear</button>
<button onClick={handleFocus}>Focus</button>
</>
);
}

Example: full element interaction

import BasisTheory from '@basis-theory/web-elements';

const bt = BasisTheory('<PUBLIC_API_KEY>');

const cardNumber = bt.createElement('cardNumber', {
placeholder: '4111 1111 1111 1111',
});

// Subscribe before mounting so you don't miss the ready event
const unsubReady = cardNumber.on('ready', () => {
console.log('element ready, mounted:', cardNumber.mounted);
unsubReady();
});

const unsubChange = cardNumber.on('change', (event) => {
const { isValid, isEmpty, cardBrand } = event.detail;
document.getElementById('submit').disabled = !isValid;
if (cardBrand) document.getElementById('brand').textContent = cardBrand;
});

await cardNumber.mount('#card-number-container');

// Update placeholder after mount
await cardNumber.update({ placeholder: 'Card number' });

// On form reset
document.getElementById('reset').addEventListener('click', () => {
cardNumber.clear();
});

// On teardown
function teardown() {
unsubChange(); // remove listener
cardNumber.unmount();
}