Bootstrap 5 Modal Submit: A Comprehensive Guide

Ruth Jul 09, 2026

In the realm of web development, Bootstrap has long been a trusted ally, offering a powerful and responsive CSS framework. One of its standout features is the modal, a dialog box that appears on top of the current page, providing an interactive way to display content. In Bootstrap 5, modals have been enhanced with new features and improved accessibility. Today, we're going to delve into the world of Bootstrap 5 modals, with a special focus on the submit functionality.

Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5

Before we dive into the submit aspect, let's ensure we have a solid understanding of Bootstrap 5 modals. They're essentially a layer of content that appears on top of the current page, with a backdrop that prevents interaction with the rest of the page. This makes them perfect for displaying important information, forms, or even full-screen images and videos.

Wizard
Wizard

Setting Up a Bootstrap 5 Modal

To get started with a Bootstrap 5 modal, you'll first need to include the Bootstrap CSS and JavaScript files in your project. Once that's done, you can create a modal using the following HTML structure:

Bootstrap 5 Classes List Cheat Sheet
Bootstrap 5 Classes List Cheat Sheet

<div class="modal fade" id="myModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">Modal title</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> ... Your content here ... </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div>

Modal Triggers and Targets

Ynex - Blazor Server Bootstrap Admin Template
Ynex - Blazor Server Bootstrap Admin Template

Modals can be triggered by various events, such as button clicks or keyboard inputs. The trigger element should have a data-bs-target attribute that points to the ID of the modal. For example, if your modal has an ID of "myModal", your trigger button would look like this:

<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">Launch demo modal</button>

Modal States and Sizes

Bootstrap 5 vs Bootstrap 4 - What's New & What Changed? - Super Dev Resources
Bootstrap 5 vs Bootstrap 4 - What's New & What Changed? - Super Dev Resources

Bootstrap 5 modals can be displayed in different states (like small, medium, large, and extra large) and sizes (fullscreen for mobile devices). You can achieve this by adding the appropriate CSS class to the modal-dialog element. For instance, to create a large modal, you would use the class "modal-dialog modal-lg".

Submitting Forms in Bootstrap 5 Modals

Modals are often used to display forms, and Bootstrap 5 makes it easy to submit these forms using the built-in modal functionality. To do this, you'll need to use the data-bs-dismiss attribute with the value "modal" on your submit button. This tells Bootstrap to close the modal after the form is submitted.

Product Gallery Code | Bootstrap 5.3 snippet for building an eCommerce gallery
Product Gallery Code | Bootstrap 5.3 snippet for building an eCommerce gallery

Preventing Form Submission on Page Refresh

By default, when a form is submitted, the browser will refresh the page. To prevent this and ensure that the modal closes as expected, you'll need to add the following JavaScript code:

a poster with words that say modal verbs in different colors and font styles
a poster with words that say modal verbs in different colors and font styles
Complete Beginner HTML Master Notes
Complete Beginner HTML Master Notes
Modal verbs
Modal verbs
the code signing made easy page is shown with a pen and seal on it,
the code signing made easy page is shown with a pen and seal on it,
the english and spanish verbs are shown in this table, which shows how to use them
the english and spanish verbs are shown in this table, which shows how to use them
a poster with words and pictures on it that say modal verbs in english
a poster with words and pictures on it that say modal verbs in english
a piece of paper with words written on it and the words modal verbs
a piece of paper with words written on it and the words modal verbs
Modal Verbs with Simple Definitions and Usage Examples
Modal Verbs with Simple Definitions and Usage Examples
the settings page for an application that shows how to find what is in the server
the settings page for an application that shows how to find what is in the server
The Top 5 German Modal Verbs
The Top 5 German Modal Verbs
english verbs
english verbs
Modal Verbs
Modal Verbs
the basic instructions for creating a web page with html and jtmll on it
the basic instructions for creating a web page with html and jtmll on it
psl scale
psl scale
#####4#####
#####4#####
MODAL VERBS
MODAL VERBS
a poster with words and pictures on it that say, i could use to describe what they
a poster with words and pictures on it that say, i could use to describe what they
An Overview of HTML5 Semantics
An Overview of HTML5 Semantics
two different types of words that are used to describe what they mean in each language
two different types of words that are used to describe what they mean in each language
Chatbot Dashboard Bootstrap Admin Panel Template with Dark  LTR Theme
Chatbot Dashboard Bootstrap Admin Panel Template with Dark LTR Theme

document.querySelectorAll('.modal form').forEach(form => { form.addEventListener('submit', event => { event.preventDefault(); form.reset(); }); });

Submitting Form Data

To actually submit the form data, you can use the Fetch API or jQuery's AJAX functionality. Here's an example using Fetch:

document.querySelectorAll('.modal form').forEach(form => { form.addEventListener('submit', async event => { event.preventDefault(); const formData = new FormData(form); const response = await fetch('/your-endpoint', { method: 'POST', body: formData }); if (response.ok) { form.reset(); form.closest('.modal').querySelector('.btn-close').click(); } }); });

And there you have it! You now know how to create and submit forms in Bootstrap 5 modals. Whether you're displaying important information or collecting user data, modals are a versatile tool in your web development kit. So go forth and build something amazing!