Post items dynamic popup image display
- By
- John Doe
- Sept 17, 2024
- 7 min read
Short Intro:
Popup is one of the great feature that a website can have. It can be used in alerts, login and registration form and other contents.
For instance, you’d like to have a popup when an image has been clicked. In order to have this, you will going to create a popup and put the image as the content of the popup. But how about if there are many images? you will most likely making popup each images which will cost too much time and you will be manually adding those. In this article we will provide a solution for this issue.
Problem:
In Jansen, there’s a page where we display all the layout of a house. The client would like to have a popup base on post that has been clicked.
Solution:
Source code :
jQuery(function($){
jQuery(‘article.ecs-post-loop.plattegronden’).on(‘click’, function(){
var plattegronden_img = $(‘img’, this).attr(‘src’);
var content = ‘<div class=”plattegronden-inner-wrapper”>’
+'<img src=”‘+plattegronden_img+'” class=”plattergronden_img_popup”>’
+'</div>’;
setTimeout(function(){
$(‘.plattegrond-popup-container .plattegrond-content’, document).html(content);
}, 300);
});
});
Explanation :
jQuery(‘article.ecs-post-loop.plattegronden’) – we select the container’s class and bind an on click event. All elements inside this container or even the element itself once click, you will be able to run an instruction what to do.
var plattegronden_img = $(‘img’, this).attr(‘src’) – this function find the image inside the target element. If you wonder what is “this”, it refers to the current clicked element. This will then get the src link of the image and store it in the pattergronden_img variable
var content = ‘<div class=”plattegronden-inner-wrapper”>’
+'<img src=”‘+plattegronden_img+'” class=”plattergronden_img_popup”>’
+'</div>’;
– Next step is to create an html structure where we’ll use the image src to insert it to the popup container. You can name a class anything you wanted to name with your container/html structure.
setTimeout(function(){
$(‘.plattegrond-popup-container .plattegrond-content’, document).html(content);
}, 300);
});
});
– This is the most magical part. In this function, we are telling the function to insert the html structure that we’ve created to the container of popup that we’ve created . .plattegrond-popup-container .plattegrond-content is the class of the popup that we’ve created. We used timeout to delay the function to be able for it to work.
Contact Details
More solutions
by ilias
You’re using JetFormBuilder and want to make sure users are at least 18 years old based on their birthday input.
by ilias
Roweij Specerijen wanted to have a filter that can choose number of products display per pagination
by ilias
This article will show you how to add dynamic code to your templates using the Elementor Shortcode widget and custom PHP code.