One Bootstrap 5 Modal, Many Contents: Using relatedTarget
August 27, 2026
- Javascript
- Bootstrap
One Bootstrap 5 Modal, Many Contents: Using relatedTarget
Say you have a grid of products, users, or FAQ items, and clicking any one of them should open a modal with details about that specific item. The lazy approach is to stamp out a separate modal per item. The right approach is one modal, filled in dynamically based on what was clicked.
Bootstrap makes this easy through event.relatedTarget — the element
that triggered the modal is passed along with the show.bs.modal event,
so you can inspect it and populate the modal before it appears.
const myModal = document.getElementById('myModal');
myModal.addEventListener('show.bs.modal', (event) => {
const trigger = event.relatedTarget; // the button that was clicked
console.log(trigger);
});Where that content actually comes from is up to you. Here are three common sources, from simplest to most scalable.
Option 1: Data attributes
The simplest approach — no lookup table needed. Put the content directly
on the trigger button as data-* attributes, then read them off
relatedTarget.
<button
type="button"
data-bs-toggle="modal"
data-bs-target="#infoModal"
data-title="Shipping"
data-body="Orders ship within 2 business days."
>
Shipping info
</button>
<button
type="button"
data-bs-toggle="modal"
data-bs-target="#infoModal"
data-title="Returns"
data-body="Returns are accepted within 30 days of delivery."
>
Returns info
</button>That's the whole pattern running live below — click either button and
watch the same modal pick up different content from relatedTarget.
This is great for short, plain-text content. It falls apart once you need more than two or three fields, or the content includes markup, arrays, or anything that doesn't fit cleanly into a string attribute.
Option 2: A JavaScript object
When the content is richer, keep it out of the DOM entirely. Store it in
a plain object keyed by an id, and use relatedTarget only to look up
which entry to show.
<button
type="button"
data-bs-toggle="modal"
data-bs-target="#userModal"
data-user-id="2"
>
View Priya
</button>The button only needs to carry an id — the actual content lives in one
place, is easy to update, and can hold nested data, arrays, whatever you
need. This scales much better than sprinkling data-* attributes across
every button.
If you build
innerHTMLfrom data that could include user input, escape it first. Only skip escaping when you fully control the source, as in the static example above.
Option 3: JSON from an API
For content that isn't known ahead of time — user profiles, product
details, anything backed by a database — fetch it when the modal opens,
using the id from relatedTarget to know what to ask for.
<button
type="button"
data-bs-toggle="modal"
data-bs-target="#productModal"
data-product-id="42"
>
View product
</button>Two things to note here. First, show.bs.modal fires before the modal
is visible, so the "Loading..." state briefly shows while the fetch is in
flight — that's expected and gives the user feedback rather than a blank
modal. Second, relatedTarget is read synchronously at the top of the
handler, before the await — grab whatever you need off it first, since
by the time the fetch resolves the DOM around it could theoretically have
changed.
The demo below stands in for the API with a promise that resolves after a short delay, so you can see the "Loading..." state the same way you would against a real endpoint.
Which one should you use?
| Content shape | Use |
|---|---|
| A couple of short strings | Data attributes |
| Known-ahead-of-time, structured data | JS object |
| Fetched from a server, unknown up front | JSON via fetch |
They're not mutually exclusive — a common pattern is a JS object as a
local cache, falling back to a fetch only when an id isn't already in
it.
Two gotchas
relatedTarget is null if the modal wasn't opened by a click. If
you show the modal programmatically —
new bootstrap.Modal(document.getElementById('myModal')).show();— there's no triggering element, so event.relatedTarget will be null
inside your show.bs.modal handler. Guard for that if your code can be
triggered either way.
Reset the modal on close. Without this, closing the modal and opening
it from a different trigger can flash the previous item's content for a
frame before your handler overwrites it. Clear it out on hidden.bs.modal:
productModal.addEventListener('hidden.bs.modal', () => {
modalBody.textContent = '';
});One modal, one set of markup, and the content is entirely driven by whatever triggered it — that's the whole trick.