How to Create Modal popup with Bootstrap 4 and Javascript

In this article we are discussing about How to Create Modal popup with Bootstrap 4 and Javascript

Must Link the bootstrap CSS, JS

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">


<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
  • Modal popups are built with HTML, CSS, and JavaScript. They’re positioned over everything else in the document and remove scroll from the so that Modal popup content scrolls instead.
  • Clicking on the modal “backdrop” will automatically close the modal popup.
  • Bootstrap only supports one modal popup window at a time. Nested modals aren’t supported as we believe them to be poor user experiences.
  • Modal popup use position: fixed, which can sometimes be a bit particular about its rendering. Whenever possible, place your modal popup HTML in a top-level position to avoid potential interference from other elements. You’ll likely run into issues when nesting a .modal within another fixed element.
  • Once again, due to position: fixed, there are some caveats with using Modal popup on mobile devices. See our browser support docs for details.
  • Due to how HTML5 defines its semantics, the autofocus HTML attribute has no effect in Bootstrap Modal popups. To achieve the same effect, use some custom JavaScript.

How to Create Modal popup with Bootstrap 4 and Javascript

popup

Toggle a working modal demo by clicking the button below. It will slide down and fade in from the top of the page.

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#customModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="customModal" tabindex="-1" role="dialog" aria-labelledby="customModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="customModalLabel">Modal Heading</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>In this which design you add </p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

Scrolling long content in Modal popup

When modals become too long for the user’s viewport or device, they scroll independent of the page itself. Try the demo below to see what we mean.

<!-- Button trigger modal start-->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#scrollingModalLong">
  Launch demo modal
</button>

<!-- Button trigger modal end-->
<!-- Modal start-->
<div class="modal fade" id="scrollingModalLong" tabindex="-1" role="dialog" aria-labelledby="scrollingModalLongTitle" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="scrollingModalLongTitle">Modal Heading</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Long Content</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
<!-- Modal end-->

Vertically centered Modal popup

Add .modal-dialog-centered to .modal-dialog to vertically center the modal.

<!-- Button trigger modal start-->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#contentModalCenter">
  Launch demo modal
</button>

<!-- Button trigger modal end-->
<!-- Modal start-->
<div class="modal fade" id="contentModalCenter" tabindex="-1" role="dialog" aria-labelledby="contentModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="contentModalCenterTitle">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
<!-- Modal end-->

Tooltips and popovers on Modal Popup

Tooltips and popovers can be placed within modals as needed. When modals are closed, any tooltips and popovers within are also automatically dismissed.

<!-- Button trigger modal start-->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#contentModalCenter">
  Launch demo modal
</button>

<!-- Button trigger modal end-->
<!-- Modal start-->
<div class="modal fade" id="contentModalCenter" tabindex="-1" role="dialog" aria-labelledby="contentModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="contentModalCenterTitle">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
        <div class="modal-body">
          <h5>Popover in a modal</h5>
          <p>This <a href="#" role="button" class="btn btn-secondary popover-test" title="Popover title" data-content="Popover body content is set in this attribute.">button</a> triggers a popover on click.</p>
          <hr>
           <h5>Tooltips in a modal</h5>
          <p><a href="#" class="tooltip-test" title="Tooltip">This link</a> and <a href="#"     class="tooltip-test" title="Tooltip">that link</a> have tooltips on hover.</p>
        </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
<!-- Modal end-->

Optional sizes

Modals have two optional sizes, available via modifier classes to be placed on a .modal-dialog. These sizes kick in at certain breakpoints to avoid horizontal scrollbars on narrower viewports.

Large modal popup

Modals have two optional sizes, available via modifier classes to be placed on a .modal-lg.

<div class="modal-dialog modal-lg">
    <div class="modal-content">
      ...
    </div>
  </div>

Small modal popup

Modals have two optional sizes, available via modifier classes to be placed on a .modal-sm.

<div class="modal-dialog modal-sm">
    <div class="modal-content">
      ...
    </div>
  </div>

Modal popup Usage

The modal plugin toggles your hidden content on-demand, via data attributes or JavaScript. It also adds .modal-open to the <body> to override default scrolling behavior and generates a .modal-backdrop to provide a click area for dismissing shown modals when clicking outside the modal popup.

Modal popup Via data attributes

Activate a modal without writing JavaScript. Set data-toggle="modal" on a controller element, like a button, along with a data-target="#foo" or href="#foo" to target a specific modal to toggle.

<button type="button" data-toggle="modal" data-target="#myModal">Launch modal</button>

Modal popup Via JavaScript

Call a modal with id myModal with a single line of JavaScript:

$('#myModal').modal(options)

Modal popup Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-backdrop="".

NameTypeDefaultDescription
backdropboolean or the string 'static'trueIncludes a modal-backdrop element. Alternatively, specify static for a backdrop which doesn’t close the modal on click.
keyboardbooleantrueCloses the modal when escape key is pressed
focusbooleantruePuts the focus on the modal when initialized.
showbooleantrueShows the modal when initialized.

Modal popup Methods

.modal(options)

Activates your content as a modal. Accepts an optional options object.

$('#myModal').modal({
  keyboard: false
})

.modal(‘toggle’)

Manually toggles a modal. Returns to the caller before the modal has actually been shown or hidden (i.e. before the shown.bs.modal or hidden.bs.modal event occurs).

$('#myModal').modal('toggle')

.modal(‘show’)

Manually opens a modal. Returns to the caller before the modal has actually been shown (i.e. before the shown.bs.modal event occurs).

$('#myModal').modal('show')

.modal(‘hide’)

Manually hides a modal. Returns to the caller before the modal has actually been hidden (i.e. before the hidden.bs.modal event occurs).

$('#myModal').modal('hide')

.modal(‘handleUpdate’)

Manually readjust the modal’s position if the height of a modal changes while it is open (i.e. in case a scrollbar appears).

$('#myModal').modal('handleUpdate')

.modal(‘dispose’)

Destroys an element’s modal.

Events

Bootstrap’s modal class exposes a few events for hooking into modal functionality. All modal events are fired at the modal itself (i.e. at the <div class="modal">).

Event TypeDescription
show.bs.modalThis event fires immediately when the show instance method is called. If caused by a click, the clicked element is available as the relatedTarget property of the event.
shown.bs.modalThis event is fired when the modal has been made visible to the user (will wait for CSS transitions to complete). If caused by a click, the clicked element is available as the relatedTarget property of the event.
hide.bs.modalThis event is fired immediately when the hide instance method has been called.
hidden.bs.modalThis event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete).
$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})

Also Read How to create collapsed sidebar with CSS and JavaScript

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *