Add new URL to the hyperlink and include CSS styles by utilizing jQuery

I have a client who wants a portfolio item on their website, built with the WordPress theme Enfold, to open in a lightbox and then link to another lightbox for additional content. I tried hard linking the portfolio item with 'www.examplepage?iframe=true', which successfully opens it in a lightbox from the main page. However, internal links open in a second lightbox on top of the first one.

To address this issue, I used jQuery to add '?iframe=true' to the link on the main page. I ensured that the link had the necessary classes: 'mfp-iframe' and 'lightbox-added'. Despite everything looking correct in the inspector, the lightbox failed to open using this method. I added this code through a plugin that inserts scripts into the header and footer.

<script>
jQuery(function($) {

$( document ).ready(function() {
  $('.lightbox a.grid-image').addClass('mfp-iframe lightbox-added');
$(".lightbox a.grid-image").attr('href', function(i) { return 
$(this).attr('href') + '?iframe=true'; }) 
});

});
</script>

The inspector displayed the expected results:

<a href="/Winery/Giesen-Wines?iframe=true/" title="" data-rel="grid-1" 
class="grid-image avia-hover-fx mfp-iframe lightbox-added" style="height: 
auto; opacity: 1;">IMAGE INSIDE LINK</a>

However, the link still opened in a new page instead of in the lightbox.

Here is an example of working code generated by the theme:

<a href="/Winery/constellation-brands?iframe=true/" title="" 
data-rel="grid-1" class="grid-image avia-hover-fx mfp-iframe lightbox- 
added" style="height: auto; opacity: 1;">IMAGE INSIDE LINK</a>

Both examples appear identical.

EDIT: The actual site is under development and inaccessible to the public. However, here is a test site that mirrors the setup:

This test page features two different portfolio grids. The first grid has the 'lightbox' class applied, so inspecting it reveals a linked image with the required classes and the link myurl?iframe=true. The second portfolio grid lacks the 'lightbox' class and does not contain the additional attributes.

Answer №1

Could you provide a website link for testing purposes?

I believe I grasp the issue at hand.

The problem seems to be that the link within the lightbox is not triggering another lightbox to open?

It's plausible that the link in the first lightbox is not receiving the click event because it is generated through JavaScript and therefore unaffected by document.ready?

You might want to consider adding a .click() function to your lightbox link so it functions properly on dynamically generated elements.

For example:

$(document).on("click","#test-element",function() {
        alert("Click event bound to document listening for #test-element");
    });

Answer №2

Great news, I believe I've cracked it! I made sure to incorporate .magnificPopup into the script. This adjustment was necessary due to the specific lightbox style utilized by the Enfold theme.

<script>
jQuery(function($) {

$(window).load(function() {
jQuery('.lightbox a.grid-image').magnificPopup({
  type:'inline',
  midClick: true
});

$('.lightbox a.grid-image').addClass('mfp-iframe lightbox-added open-popup-link');
$('.lightbox a.grid-image').attr('rel', 'lightbox')
$(".lightbox a.grid-image").attr('href', function(i) { return $(this).attr('href') + 
'?iframe=true/'; }) 

});

});
</script>

Now, when clicking on the links in the portfolio, they seamlessly open in a lightbox without manually inserting ?iframe=true at the end of each link.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The bottom section is not displaying properly at the conclusion of the div slide

The division ends without displaying the footer. <div id="fullpage"> <div class="section " id="section0"> content1 </div> <div class="section " id="section0"> content1 </div> <div class="section " id="section0"> c ...

Starting up the TinyMCE editor after an AJAX call in Rails 4

I am currently utilizing the tinymce-rails gem, which essentially integrates the jquery plugin. Everything functions properly until I display the page via an ajax response. Despite attempting to re-initialize tinymce, it continues to not work. Within my c ...

PHP time counting script

Can a countdown script be created using PHP? Websites like 4shared.com or megaupload.com utilize a similar script that starts counting from 20 seconds to 0. I am able to see how many seconds are left on the web page before I can click on the download but ...

The process of retrieving data from a dropdown list and populating checkboxes using Ajax/Jquery

I'm looking for a way to dynamically retrieve data in checkboxes based on a selected user from a dropdown list. The dropdown list contains users, and the checkboxes display pages data. Assuming we have a table called user with columns user_id and use ...

Comparison of loading time between loader gif and browser's loading image

Recently, I've come across a code snippet where I implemented a 'loader' gif to show while PHP retrieves data from an SQL database. Everything seemed to be working fine when testing on my localhost. However, upon closer observation, I notice ...

The magnifying glass icon is missing from the autocomplete search feature

After creating an autocomplete search functionality that queries my mysql database, I encountered a slight issue. Here is the code snippet showcasing my implementation: <div class="search-bar"> <div class="ui-widget"> <input id="ski ...

Tips for moving directly to a section while maintaining a set distance from the top of the page

I'm currently implementing code for a smooth scroll to a section when clicking on a navigation menu item: // handling links with @href starting with '#' $(document).on('click', 'a[href^="#"]', function(e) { ...

Method for verifying if the date has been altered within the specified date range picker

I am currently implementing the Bootstrap Date Range Picker and have a requirement that whenever a user changes the date range, the form should be submitted to fetch fresh data. Below is the code snippet I am using: $(document).ready(function (){ var s ...

Adjust the size of an image post-render with the help of Angular or jQuery

Below is my current HTML code: <img width="150" height="150" src="https://d.pcd/image/578434201?hei=75&amp;wid=75&amp;qlt=50,0&amp;op_sharpen=1&amp;op_usm=0.9,0.5,0,0" ng-src="https://d.pcd/image/578434201?hei=75&amp;wid=75&amp; ...

What is the best way to split text copied from a textarea into <p> paragraphs with an equal number of characters in each?

Check out this JSFiddle version I've found a JSFiddle example that seems perfect for my current project needs. However, I'm wondering how to modify the code to ensure that each paragraph is evenly divided with the same number of characters and a ...

Utilize AJAX to send a file within an HTTP request

In my attempt to send a form using an Ajax request, I encountered a challenge with uploading files. Here is a snippet of my form: <form> {{csrf_field()}} <div class="form-group"> <label for="company-name">Co ...

Stop :hovering on the link for iPad

My webpage contains sublinks with CSS properties as follows: #leftNav .searchBySub {...} #leftNav a.searchBySub:hover {...} #leftNav .searchBySubClicked {...} However, I have observed that on the iPad, the :hover styles are being applied. Is there a wa ...

Considering the development of a web application similar to Canva

I'm interested in creating an app similar to Canva, but I'm not sure where to begin. I have a solid understanding of HTML and CSS, but my JavaScript skills are basic. I'm curious about the technologies they use. Is there a way to save HTM ...

Tips for cropping an image using CSS with dimensions specified for width, height, and x and y coordinates

As I work on implementing a cropping feature using react-easy-crop, my goal is to store the user's image along with pixel coordinates that dictate their preferred cropping area. My objective is to utilize the parameters provided by react-easy-crop in ...

What steps should I take to align the image in the middle left according to my third paragraph?

Looking to format my table row with the image on the left middle and three paragraphs in the center. The image should align with the text, but currently it is not aligned due to its positioning between "Nom" and "Type." <div class="my-container"> ...

How can I quickly submit this form with jQuery?

Alright, so I believed I had everything figured out but turns out I was mistaken. Essentially, there's a form wrapped in a span (#container) and embedded within a page. The server-side handles all the validation and determines whether it's a ful ...

Error: JSON parsing error encountered at position x due to unexpected token

While I understand this question has been asked multiple times before, the situation here is unique. I am utilizing getJSON to retrieve data from a Database. The returned result is a valid JSON (validated by various JSON validators), but I am encountering ...

Ensure that the header stands out by creating a grey background for the rest

https://i.sstatic.net/XUCkS.png Is there a way to change the background color of this layout to grey, excluding the top bar and bottom footer? The layout is structured like this: <html> <head> <body> <div id="big_wrapper"> ...

Efficiently pinpointing the <div> element with precision using jQuery

I am building an interactive quiz for users of my app. The questions are table based, and can be answered "yes," "no," or "maybe." If the user answers "no" or "maybe," I would to slide open an informational panel that lives beneath the table. Here is the ...

What is the best way to use a JavaScript function as a callback?

I'm struggling with understanding callbacks, especially how they function. Here is my function: function checkDuplicateIndex(values, callback) { $.ajax({ type: "POST", url: url, data: "command=checkIndexAlbumTracks& ...