Twitter Bootstrap Modal - IsShown
Asked Answered
R

6

21

Hello Fellow SO users,

Got this problem where i auto populate a modal box.

Sometimes it already has content, so i tried doing a hide/show on each request. But the show is fired before the hide function is done, so it breaks the script.

I can't do a bind to "hidden", because if it's the first time - it won't fire the hidden function from bootstrap.

Using modal('true') i can see the object has a isShown element, but does anyone know how i can access it?

The console.log shows this:

$backdrop
    [div.modal-backdrop]

$element
    [div#modal-from-dom.modal]

isShown
    true

settings
    Object { backdrop="static", keyboard=true, show=false}

hide
    function()

show
    function()

toggle
    function()

__proto__
    Object { toggle=function(), show=function(), hide=function()}
Ricoriki answered 6/1, 2012 at 9:9 Comment(0)
M
26

Answer for Twitter Bootstrap 3:

$("element").data()['bs.modal'].isShown

or

$("element").data('bs.modal').isShown
Morass answered 13/10, 2013 at 7:24 Comment(0)
R
11

The answer is:

$("element").data('modal').isShown
Ricoriki answered 10/1, 2012 at 10:10 Comment(2)
But you cannot check this way if you've never shown the modal before. This only works if you have already once opened a modal window. Limited way.Vernation
if ($("element").data('modal') && $("element").data('modal').isShown) { worked fine for me }Epigeal
S
3

On bootstrap 3.0.x

  $('#modal_Id').data().modal.isShown

or

  $('#modal_Id').data('modal').isShown

modal_id is the id of your modal

Sailing answered 23/11, 2013 at 9:51 Comment(0)
T
1

If you'd like a Bootstrap version 2 and 3 solution and would rather not hit the data (since it looks like the name already changed once)...

$(element).hasClass('in') (would be "faded in" or "visible"; a plus that it returns a boolean)

or

"false" === $(element).attr('aria-hidden') (so that's aria-hidden or visible as well. "true" for hidden in this case.)

See source from bootstrap 3.3.1 here:

this.backdrop(function () {
...
  that.$element
    .addClass('in')
    .attr('aria-hidden', false)
...

Again, that code is from 3.3.1. Can confirm this also works in 2.1.0. Duck sniffing [sic] is probably better in this case.

Tinderbox answered 24/6, 2014 at 19:12 Comment(0)
G
0

Answer for Bootstrap 4:

$("element").data('bs.modal')._isShown

As a function:

function isModalShown(modal) {
    var modalIsShown = (modal.data('bs.modal') || {})._isShown;
    return !(typeof modalIsShown === 'undefined' || !modalIsShown);
}
Gemot answered 6/6, 2018 at 10:29 Comment(1)
I'm just checking if modal has 'shown' class, is this better in some way? It works as expected for me ..Anyone
P
0

You can use this:

$('#mymodal').on('shown.bs.modal', function (e) {
    console.log("Modal is shown");
//your other codes here...
})
Patio answered 30/7, 2022 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.