Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close Bootstrap modal only when user clicks on close image?

I want to close modal ONLY when user clicks on close btn. As I see I need to overwrite this part of code from modal.js:

 hide: function (e) {     e && e.preventDefault()      var that = this      e = $.Event('hide')//if I delete this line modal won't hide      this.$element.trigger(e)      if (!this.isShown || e.isDefaultPrevented()) return      this.isShown = false      $('body').removeClass('modal-open')      escape.call(this)      this.$element.removeClass('in')      $.support.transition && this.$element.hasClass('fade') ?       hideWithTransition.call(this) :       hideModal.call(this) 

Am I on the right path?

like image 636
MID Avatar asked Oct 01 '12 08:10

MID


People also ask

How do you close a modal by clicking on a button?

To close a modal using vue. js you can use the click event i.e. @click to trigger change in modal visibility. So, whenever the close button is pressed the @click method will trigger the function associated with the action ( here, hiding the modal ).

How do I close a Bootstrap modal?

Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time.

How do I stop Bootstrap modal close Onclick outside?

When the Button is clicked, the HTML DIV is referenced using jQuery and its modal function is called along with properties data-backdrop: "static" and data-keyboard: false which disables the closing of the Bootstrap Modal Popup when clicked outside.

How do I hide the modal when I click outside?

Another way to dismiss the modal when clicking outside involved taking advantage of the bubbling nature of the javascript events. clicking on . modal will cause the click event to propagate like this . modal -> #modal-root -> body while clicking outside the modal will only go through #modal-root -> body .


2 Answers

You can define your modal behavior, defining data-keyboard and data-backdrop.

 <div id="modal" class="modal hide fade in" data-keyboard="false" data-backdrop="static"> 
like image 177
Tabares Avatar answered Oct 12 '22 23:10

Tabares


When you launch your modal you can pass the options:

{   keyboard: false,   backdrop: 'static' } 

which will disable closing the modal by clicking the backdrop, and the escape-button. Or they can be set as data-attributes.

like image 20
PerfectlyNormal Avatar answered Oct 12 '22 23:10

PerfectlyNormal