how to popup image over the screen after clicking the image

sharvanaz picture sharvanaz · Jun 15, 2016 · Viewed 12.8k times · Source

In my code, there is a lot of image in a table. I want to click a photo & then the photo will popup in a large size over the screen. But I do not have much idea about javascript & jquery. I try to do this but the problem is it popup a box but don't show the picture. I know that there is a problm in script to show the picture. can u please tell me where is the wrong or give me a proper code for popup image after click.

my header link is below. is there need any other link to be added??

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

    <link rel="stylesheet" type="text/css" href="http://w2ui.com/src/w2ui-1.4.2.min.css" />
    <script type="text/javascript" src="http://w2ui.com/src/w2ui-1.4.2.min.js"></script>

here is html part:

<div>
    <td> <img class="btn" onclick="popup()" style="width:100px; height:100px; border-radius:4px;" src="upload/<?php echo $row['image'];?>"></img></td>
</div>

here is the javascript:

<script type="text/javascript">
    function popup() {
        w2popup.open({
            title: 'Image',
            body: '<div class="w2ui-centered"><img src="upload/<?php echo $row['image'];?>"></img></div>'
        });
    }
</script>

can anyone please help me how to show image in the pop up box. thanks in advance.

Answer

Mike Scotty picture Mike Scotty · Jun 15, 2016

Instead of the onclick attribute, you could assign a class popup_image to your img and attach a click handler when the DOM is ready.

I took the liberty to remove scripts and tags that were not neccessary to demonstrate the result.

$(document).ready(function() {

  $(".popup_image").on('click', function() {
    w2popup.open({
      title: 'Image',
      body: '<div class="w2ui-centered"><img src="' + $(this).attr('src') + '"></img></div>'
    });
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="http://w2ui.com/src/w2ui-1.4.2.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://w2ui.com/src/w2ui-1.4.2.min.css" />

<img class="btn popup_image" style="width:100px; height:100px; border-radius:4px;" src="http://lorempixel.com/g/200/200/"></img>