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>

Answer from Mike Scotty on Stack Overflow
🌐
W3Schools
w3schools.com › howto › howto_css_modal_images.asp
How To Create Modal Images
Alert Buttons Outline Buttons Split Buttons Animated Buttons Fading Buttons Button on Image Social Media Buttons Read More Read Less Loading Buttons Download Buttons Pill Buttons Notification Button Icon Buttons Next/prev Buttons More Button in Nav Block Buttons Text Buttons Round Buttons Scroll To Top Button · Login Form Signup Form Checkout Form Contact Form Social Login Form Register Form Form with Icons Newsletter Stacked Form Responsive Form Popup Form Inline Form Clear Input Field Hide Number Arrows Copy Text to Clipboard Animated Search Search Button Fullscreen Search Input Field in Navbar Login Form in Navbar Custom Checkbox/Radio Custom Select Toggle Switch Check Checkbox Detect Caps Lock Trigger Button on Enter Password Validation Toggle Password Visibility Multiple Step Form Autocomplete Turn off autocomplete Turn off spellcheck File Upload Button Empty Input Validation
🌐
GitHub
github.com › vishwajeet71092 › Image-popup-js-plugin
GitHub - vishwajeet71092/Image-popup-js-plugin: A javascript Plugin to pop up image of webpage
Paste this script before closing body tag and after all js and target value will be the class of images · <script> //for image having class imagePopupInit({ type: 'imageClass', target: 'popimg' }); </script> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Image Popup Javascript</title> <!-- styling this page --> <style> body{ text-align: center; font-family: Arial, Helvetica, sans-serif; padding: 10px 50px; } img{ max-width: 150px; margin: 20px; } #image-pop{ border: 1px solid red; margin:20px 0px; } .popimg{ border: 4px solid #ff9800; } </style> <!-- link image-popup.mi
Forked by 2 users
Languages   CSS 100.0% | CSS 100.0%
🌐
Dimsemenov
dimsemenov.com › plugins › magnific-popup
Magnific Popup: Responsive jQuery Lightbox Plugin
Magnific Popup does not - feel free to use relative units like EM's or resize lightbox with help of CSS media queries. Update content inside lightbox without worrying about how it'll resize and center.
🌐
CodeProject
codeproject.com › Articles › 32819 › JavaScript-Image-Popup
JavaScript Image Popup - CodeProject
Create a DIV named "imgbox" on the HTML page on which your thumbnail images will be shown. The DIV and the CSS element ID associated with the DIV is shown below: ... #imgbox { vertical-align : middle; position : absolute; border: 1px solid #999; ...
🌐
CodePen
codepen.io › Muhnad › pen › dMbXNb
image popup
$(function () { "use strict"; $(".popup img").click(function () { var $src = $(this).attr("src"); $(".show").fadeIn(); $(".img-show img").attr("src", $src); }); $("span, .overlay").click(function () { $(".show").fadeOut(); }); });
🌐
Bqardi
bqardi.dk › tutorials › js › image-popup › index.php
Image Popup
This tutorial is about creating a simple image popup window or modal image if you prefer.
Find elsewhere
🌐
DEV Community
dev.to › salehmubashar › create-an-image-modal-with-javascript-2lf3
Create an Image modal with JavaScript! - DEV Community
December 31, 2021 - In this tutorial, we will be creating a popup image modal using javascript. The basic idea is that when a user clicks an image, a larger version should be opened in a modal.
🌐
Cloudinary
cloudinary.com › home › mastering javascript image popup
Mastering JavaScript Image Popup | Cloudinary
June 23, 2024 - With the loadImages function now complete, we retrieve references to the modal (modal), image (modalImg), caption (captionText), and close button (span). We then add an onlick function to the span, which, when clicked, sets the modal’s display style is set to "none" to hide it. Here is what our complete JS file looks like:
🌐
GitHub
github.com › kateFrontend › Popup-Image-Gallery
GitHub - kateFrontend/Popup-Image-Gallery: Vanilla JavaScript Popup Image Gallery
Vanilla JavaScript Popup Image Gallery · View Demo Here · HTML5 / CSS3 / JavaScript · Application is responsive for all mobile devices.
Author   kateFrontend
🌐
Groupthought
help.groupthought.com › pipeline documentation › tutorials › how to show an image in a pop-up window
How to show an image in a pop-up window - Pipeline Documentation
<a class="image-link" href="https://cdn.shopify.com/s/files/1/2018/8867/files/matteo-paganelli-39971_800x.jpg"><img src="https://cdn.shopify.com/s/files/1/2018/8867/files/matteo-paganelli-39971_200x.jpg"></a> Result: Additional Customization · Pipeline uses Magnific popup. The code you add to shop.js is to initialize the popup based on a link class.
🌐
Willmaster
willmaster.com › library › features › image-popup.php
Image Popup
The JavaScript needs to be available on the page and the HTML and CSS for both the popup and thumbnail need to be coded. ... Here is the JavaScript. No customization required. <script type="text/javascript"> /* Image Popup Handler Version 1.0 May 23, 2016 Will Bontrager Software LLC https://www.willmaster.com/ Copyright 2016 Will Bontrager Software LLC This software is provided "AS IS," without any warranty of any kind, without even any implied warranty such as merchantability or fitness for a particular purpose.
🌐
jQuery Script
jqueryscript.net › jquery plugins › jquery lightbox plugins
Minimalist Image Lightbox Popup Plugin - jQuery image-popup.js | Free jQuery Plugins
image-popup.js is a dead simple & CSS less jQuery image lightbox plugin which could be used to overlays your images on the top of the webpage.
Top answer
1 of 3
3

Try this:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
</head>
<body>

<html>
<head> 
 <SCRIPT language="JavaScript"> 

var w = 480, h = 340;


function openWindow(){
if (document.getElementById) {
   w = screen.availWidth;
   h = screen.availHeight;
}  

var popW = 800, popH = 700;

var leftPos = (w-popW)/2;
var topPos = (h-popH)/2;



msgWindow = window.open('','popup','width=' + popW + ',height=' + popH + 
                         ',top=' + topPos + ',left=' + leftPos + ',       scrollbars=yes');

msgWindow.document.write 
    ('<HTML><HEAD><TITLE>Centered Window</TITLE></HEAD><BODY><FORM    NAME="form1">' +
    '<img src="https://static5.cargurus.com/images/site/2009/10/24/14/42/2004_suzuki_vitara_4_dr_lx_4wd_suv-pic-8731393806365188898-640x480.jpeg">'+
    ' <H1>Notice the centered popup window.</H1>This is the ordinary HTML' + 
    ' document that can be created on the fly.  But the window is centered   in ' +
    ' the browser.  Click the button below to close the window.<br />' +
    '<INPUT TYPE="button" VALUE="OK"onClick="window.close();"></FORM></BODY>   </HTML>');
}

</script>
<form>
<input type="button" onClick="openWindow()" value="Click Me">
</form>


</body>
</html>
2 of 3
3

I strongly recommend against using document.write();, use document.body.innerHTML instead.

precisely use:

msgWindow.document.body.innerHTML = '<img src="url/to/your/image.jpg"></img>';

instead of:

msgWindow.document.write('<HTML><HEAD><TITLE>Centered Window</TITLE></HEAD><BODY><FORM NAME="form1">' +
  ' <H1>Notice the centered popup window.</H1>This is the ordinary HTML' +
  ' document that can be created on the fly.  But the window is centered   in ' +
  ' the browser.  Click the button below to close the window.<br />' +
  '<INPUT TYPE="button" VALUE="OK"onClick="window.close();"></FORM></BODY>   </HTML>');

Here's a working example using an image.

🌐
Pcurtis
pcurtis.com › popup_advanced.htm
Advanced JavaScript Popup Windows for Images
You will also find that the image window is closed when the page is left (unloaded in the jargon), few sites tidy up extra windows. The basic code above for the five parameter popup popitup5() formed the basis of all the popups on my web sites for a couple of years with the only significant development being in the way it was used. The code was put in an include file (.js) at the top of every page which had popups for images or feedback forms.
🌐
SitePoint
sitepoint.com › javascript
How to open image in popup window?
June 30, 2016 - Hi I want to open selected Image on popup window this is I have tried so far there are two things that I am not able to do so far 1)When I click on Image at bottom image in popup window not display on full window.I have to scroll up to see that Image 2)I want to disable click on window when Image in open in popup window only clicking on Close will dismiss popup window.
🌐
jQuery Script
jqueryscript.net › jquery plugins › jquery lightbox plugins
Image Modal With Zoom In/Out - jQuery image-popup.js | Free jQuery Plugins
image-popup.js is a lightweight and fast jQuery image lightbox plugin that displays images in a modal popup with zoom in and zoom out capabilities.
🌐
CodeWithRandom
codewithrandom.com › 2024 › 10 › 01 › create-onclick-image-popup-js
How to Create Onclick Image Popup JavaScript - CodeWithRandom
October 1, 2024 - This code is a jQuery script that creates a popup for displaying images when you click on them in a gallery. The first line of code define that when the whole document is ready, then the JS code will run.