Skip to content

Instantly share code, notes, and snippets.

@anwerashif
Created December 28, 2017 18:51
Show Gist options
  • Save anwerashif/ba7cbbb0feed3e5b7c46c3f507f11f7c to your computer and use it in GitHub Desktop.
Save anwerashif/ba7cbbb0feed3e5b7c46c3f507f11f7c to your computer and use it in GitHub Desktop.
jQuery Modal Image
jQuery( document ).ready(function(){
// Add Modal HTML to body
jQuery('body').append( "<div id=\"myModal\" class=\"modal\"><span class=\"close\">&times;</span><img class=\"modal-content\" id=\"img01\"><div id=\"caption\"></div></div>");
// Get the image and insert it inside the modal - use its "alt" text as a caption
jQuery(".img-frm img").on("click", function(){ // Change the class name ".img-frm img" with yours
var image = jQuery(this).attr("src");
var captext = jQuery(this).attr("alt");
jQuery(".modal").css("display","block");
jQuery("#img01").attr("src", image);
jQuery("#caption").html( captext );
// When the user clicks on <span> (x), close the modal
jQuery("#myModal .close").on("click", function(){
jQuery(".modal").css("display","none");
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment