Skip to content
the poor coder

bootstrap modal popup disable click outside

Poor Man
Published
Reading time
2 min read
On this page

How to Disable Click Outside of Bootstrap Modal Popup?#

If you are using Bootstrap Modal Popup in your web application, you might want to disable the click outside of the modal popup. This will prevent the user from closing the modal by clicking outside of it.

There are a few ways to achieve this:

Method 1: Using data-backdrop Attribute#

You can use the data-backdrop attribute to disable the click outside of the modal. Set the value of the attribute to "static".

Text
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>Modal body text goes here.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

In the above code, the data-backdrop attribute is set to "static". This will disable the click outside of the modal.

Method 2: Using jQuery#

You can also disable the click outside of the modal using jQuery. Here's an example:

Text
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>Modal body text goes here.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

<script>
  $(document).ready(function(){
    $('#myModal').modal({
      backdrop: 'static',
      keyboard: false
    });
  });
</script>

In the above code, we are using the modal() method of jQuery to initialize the modal. We are setting the backdrop property to "static" and keyboard property to false. This will disable the click outside of the modal and also prevent the user from closing the modal using the keyboard.

These are the two ways to disable click outside of Bootstrap Modal Popup. Choose the one that suits your requirements the best.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC