bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Modals don't work

Open kg opened this issue 14 years ago • 9 comments

I copied and slightly modified the example modal from the website:


    <div class="modal" id="github_login">
      <div class="modal-header">
        <button class="close" data-dismiss="modal">x</button>
        <h3>Log in to GitHub</h3>
      </div>
      <div class="modal-body">
        <p>One fine body</p>
      </div>
      <div class="modal-footer">
        <button href="#" class="btn">Close</a>
        <button href="#" class="btn btn-primary">Save changes</a>
      </div>
    </div>

And I included the necessary CSS and JS:

    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
    <script src="bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
    <script src="bootstrap/js/bootstrap.modal.js" type="text/javascript"></script>

The modal starts out visible on page load, none of the UI in it works, and attempts to show/hide it using:

$('#github_login').modal(...);

Silently fail (no errors).

It would be cool if there were a complete example with a working modal in it so that I could look at that instead of having to dig through the entire source code to the one example I can find (the main page for Bootstrap). It would also be cool if .modal() didn't fail silently.

kg avatar Apr 28 '12 04:04 kg

Are you setting .modal inside $(document).ready()?

The modal works fine for me.. Also, its included in bootstrap.js so you dont need bootstrap.modal.js

scarvell avatar Apr 28 '12 07:04 scarvell

it's inside an onLoad handler, and the onload is running after all the scripts have loaded.

kg avatar Apr 28 '12 07:04 kg

You're including the modal JS twice. Remove either the minified JS or the modal JS and you should be fine.

mdo avatar Apr 28 '12 19:04 mdo

It probably doesn't matter, but the version of jQuery that comes with the Bootstrap 2.0.3 .zip distribution is 1.7.1, not 1.7.2. I thought about using 1.7.2, but decided to keep the version the same as what Bootstrap ships with. Also, I prefer using the jQuery hosting site, http://code.jquery.com. The site includes links to the very latest version and it has a complete archive of all previous versions. It also includes jQuery Mobile and jQuery UI.

mchiocca avatar May 01 '12 16:05 mchiocca

You do not have a link that directly relates to the modal itself.

You need a link has the href="#id of the modal"

and add in the div of the modal, an ID, id="id of the modal"

and then you need to add hide class to modal so it doesn't fire immediately on pageload.

For example, from you link posted before,

<a href="#github_login" data-toggle="modal">CLICK ME</a>
<div class="modal" id="github_login">
      <div class="modal-header">
        <button class="close" data-dismiss="modal">x</button>
        <h3>Log in to GitHub</h3>
      </div>
      <div class="modal-body">
        <p>One fine body</p>
      </div>
      <div class="modal-footer">
        <button href="#" class="btn">Close</button>
        <button href="#" class="btn btn-primary">Save changes</button>
      </div>
    </div>

I did not have to set anything within the document to get these to fire at all. Just be sure to load the bootstrap.js at the bottom of your page and it should work.

You never close buttons with a </a> tag

Look at working example. http://jsfiddle.net/epiceon/GKey5/11/

AegirisSystems avatar May 02 '12 00:05 AegirisSystems

The hide class was probably it - I didn't know I had to add that and I didn't see it in the documentation/examples. I would have expected the $("foo").modal() call to hide the modal initially.

I also wasn't using a link to open the modal, I was using a button, so the link information wouldn't really help - I was trying to use the API to show the modal instead. Is that not a supported use case?

kg avatar May 02 '12 01:05 kg

You can use a button also if you'd like. just as long as it has data-toggle="modal" in it.

Look again. http://jsfiddle.net/epiceon/GKey5/12/

AegirisSystems avatar May 02 '12 01:05 AegirisSystems

You must load the .js file where the modal is called, before the Twitter modal.js file for this to work properly. I was getting the exact same problem until I changed it. To clarify, modal works on either links or buttons, too. Hope this helps.

kitbeck avatar May 11 '12 18:05 kitbeck

Closing out. Please open a new issue if any new problems crop up. Using the API method is totally legit, but I believe for now we require that class, too.

mdo avatar May 25 '12 03:05 mdo