Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change where bootstrap "modal-open" class is assigned to instead of the body [closed]

Currently

modal-open

is assigned to the body when the modal is opened. How do i change where the class is added to instead of the body.

Thanks for any help.

like image 572
Third_Hyperion Avatar asked Oct 13 '14 08:10

Third_Hyperion


People also ask

How do I stop a Bootstrap modal from closing?

If you want to prevent boostrap modal from closing by those actions, you need to change the value of backdrop and keyboard configuration. The default value of backdrop and keyboard is set to true . You can change the configuration in HTML or Javascript directly.

How do I stop Bootstrap modal from close on click outside?

Static backdrop When backdrop is set to static, the modal will not close when clicking outside it.

How do you reset the modal when it gets closed and open it fresh again?

The Best Answer is reset is dom build-in funtion, you can also use $(this). find('form')[0]. reset(); And Bootstrap's modal class exposes a few events for hooking into modal functionality, detail at here.

How do I not close the modal when I click 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.


1 Answers

You have to change js that adds modal-open. It's a bit strange as behaviour, anyway you can do it softly or brutally.

Softly: intercept modal click for show and remove class to body adding to another element:

// Modal is first added to the body element and then removed and added to another element

$('#myModal').on('shown.bs.modal', function (e) {
  $('body').removeClass('modal-open');
  $('#myCustomElement').addClass('modal-open');
})

and then intercept also the close of the modal and add an execution of a second remove:

$('#myModal').on('hidden.bs.modal', function (e) {
   $('#myCustomElement').removeClass('modal-open');
})

Brutally method, to avoid that modal-open it's unnecessary added to body element: go in TwitterBootstrap directory > js Open modal.js Search for modal-open , you will find (on version 3.2.0):

in

 Modal.prototype.show

you'll find

this.$body.addClass('modal-open')

in Modal.prototype.hide you'll find

this.$body.removeClass('modal-open')

you have to change the target of addClass and removeClass and replace those instructions, if you have a fixed element it's enough simple (Ex: $('#myFixedElement').addClass('modal-open'), if it's a dynamic element it's complex and you'll have again to intercept Show function to add a global var in js to let modal.js know and read your target element. However you can also change the signature of those functions but i don't really suggest you to do that, basically you'll make your code too hard to maintain.

like image 133
MrPk Avatar answered Sep 23 '22 14:09

MrPk