How to work with Bootstrap modal dialog window using jQuery?

Votes : 3.9
4499

The Bootstrap Modal are multi-purpose streamlined, but flexible (responsive), dialog prompts with the minimum required functionality, looking smart. It can be used to display alert popups, form, videos, and images gallery in a website.

Coding step by step

.1 Do you have already included bootstrap libraries. Specially bootstrap.js or bootstrap.min.js and bootstrap.css or bootstrap.min.css files on your project. If not include it.

.2 The Bootstrap Modal is divided into three primary sections.

  • header - modal title, close button
  • body - modal content ( form, video, images gallery, message)
  • footer - submit button, cancel button
<!-- end modal -->
<div id="myModal" class="modal fade" role="dialog" >
    <!-- modal-dialog -->
    <div class="modal-dialog">
        <!-- modal-content -->    
        <div class="modal-content">
            <!-- modal-header -->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title">Modal title</h4>
            </div>
            <!-- end modal-header -->
            <!-- modal-body -->
            <div class="modal-body">
                <p>One fine body…</p>
            </div>
            <!-- end modal-body -->
            <!-- modal-footer -->
            <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>
            <!-- end modal-footer -->
        </div>
    <!-- end modal-content -->
    </div>
    <!-- end modal-dialog -->
</div>
<!-- end modal -->

The modal class help to identifies, The div as a bootstrap model and brings focus to it. In & out fade effect applied by using fade class. Remove this class if you do not want this effect.

The attribute role="dialog" improves accessibility for people using screen readers.

You can change the size of the modal by adding additional class into modal-dialog class div.

  • modal-sm - for small modals
  • modal-lg - for large modals

.3 If you browse, Cannot see it yet. Need event or something to pop-up.

<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal">Popup Modal</button>
 <!-- /.or -->
<a href="#myModal" data-toggle="modal"><span aria-hidden="true" class="glyphicon glyphicon-comment"></span> Modal</a>

Now you can see, button tags use data-target attribute and also anchor tags use href attribute to point The #myModal id witch is modal div container and trigger to popup on it's click events.

Button (<button>) and anchor (<a>) tags have data-toggle="modal" attribute. It's mined opens the modal window.

| Anchor tag

Manually can show & close boostrap modal in jQuery.

$(document).ready(function () {
    $('.open').click(function () {
        $('#myModal').modal('show'); // popup #myModal id modal
    });
    $('.close').click(function () {
        $('#myModal').modal('hide'); // close #myModal id modal
    });
    $('.toggle').click(function () {
        $('#myModal').modal('toggle');
    });
});
asked 4 months,13 days ago

What is the codrate ?

codrate.com is a standard, fast cross browsing and highly versatile site. It is useful for many large number of Program Development Industries. So you can get support form Codrators , who are the codrate's joiners around world to help your program developments, You can answer other codrator's questions. Communicate with them. Share your knowledge with them. Do you have an interest in programming, So publish your articles about programming. It will help to maintain your professional co-profile. Actually codrate.com is not such as a regular web site. It will be gave new experience, best narrow cross-browser view, reduce processing time to receive browsing request, it's mean do not wasting your time to browsing codrate's web pages because it has been upgrade always modern coding ways. So, what do you waiting for ?. Try your own.

Copyright 2015 Pride - Company. Design by Esila