Need to load image as modal on click in bootstrap css

19,199

Try this

HTML

 <!-- Image trigger modal -->
       <img src="http://sheshtawy.files.wordpress.com/2010/05/extra-firefox.png" class="img-responsive img-thumbnail" style="min-height:300px;height:300px;" alt="Responsive image">
           <img src="http://sheshtawy.files.wordpress.com/2013/03/cairo_pollution.jpg?w=250"/>
           <img src="http://sheshtawy.files.wordpress.com/2012/12/img_4749.jpg?w=250"/>
      <!-- Modal -->
      <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
              <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
          <div class="modal-body">
            <img id="mimg" src="">
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

Script

$(window).load(function(){
    $('img').on('click',function()
                {
                    var sr=$(this).attr('src'); 
                    $('#mimg').attr('src',sr);
                    $('#myModal').modal('show');
                });
});

DEMO

Share:
19,199
Govan
Author by

Govan

Loves Javascript

Updated on July 26, 2022

Comments

  • Govan
    Govan almost 2 years

    I have tried loading a modal image on clicking a image by fading the background.

    It works for me. But for multiple images how can I load the image which I have clicked based on the image id without writting multiple modal div.

     <!DOCTYPE html>
     <html>
    
       <head>
         <title>Sample</title>
         <meta name="viewport" content="width=device-width, initial-scale=1.0">
         <!-- Bootstrap -->
         <link href="./dist/css/bootstrap.css" rel="stylesheet">
         <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
         <script src="https://code.jquery.com/jquery.js"></script>
         <!-- Include all compiled plugins (below), or include individual files as needed -->
         <script src="./dist/js/bootstrap.js"></script>
       </head>
    
       <body>
         <form class="form-horizontal" role="form">
           <!-- Image trigger modal -->
           <img src="http://sheshtawy.files.wordpress.com/2010/05/extra-firefox.png" id="img1" class="img-responsive img-thumbnail" style="min-height:300px;height:300px;" data-toggle="modal" data-target="#myModal" alt="Responsive image">
    
          <!-- Modal -->
          <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
              <div class="modal-content">
                <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                  <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                </div>
              <div class="modal-body">
                <img src="http://sheshtawy.files.wordpress.com/2010/05/extra-firefox.png">
              </div>
            </div><!-- /.modal-content -->
          </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->
      </form>
    </body>
    </html>
    
  • Harlin
    Harlin about 7 years
    How do you make this work when your images are using relative paths?