The JavaScript code malfunctions when I introduce a new HTML tag

I am attempting to create a simple photo gallery using PhotoSwipe. However, I have encountered an issue where certain functions like Previous and Next buttons stop working when I add new HTML elements.

Here is the original code:

<div class="my-gallery" itemscope itemtype="http://schema.org/ImageGallery">

<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
  <a href="https://farm3.staticflickr.com/2567/5697107145_a4c2eaa0cd_o.jpg" itemprop="contentUrl" data-size="1024x1024">
      <img src="https://farm3.staticflickr.com/2567/5697107145_3c27ff3cd1_m.jpg" itemprop="thumbnail" alt="Image description" />
  </a>
   <figcaption itemprop="caption description">Image caption  1</figcaption>

</figure>

<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
  <a href="https://farm2.staticflickr.com/1043/5186867718_06b2e9e551_b.jpg" itemprop="contentUrl" data-size="964x1024">
      <img src="https://farm2.staticflickr.com/1043/5186867718_06b2e9e551_m.jpg" itemprop="thumbnail" alt="Image description" />
  </a>
  <figcaption itemprop="caption description">Image caption 2</figcaption>
</figure>

This is the code from my modified photo gallery:

<div class="my-gallery" itemscope itemtype="http://schema.org/ImageGallery">

    <div class="col-sm-3 col-md-2 thumb-container" data-image="o3gx9ze57" data-hotlink="9x16er3a7" data-sid="s25">
        <div class="thumb">

            <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
            <a href="https://farm3.staticflickr.com/2567/5697107145_a4c2eaa0cd_o.jpg" itemprop="contentUrl" data-size="1024x1024">
            <img src="https://farm3.staticflickr.com/2567/5697107145_3c27ff3cd1_m.jpg" itemprop="thumbnail" alt="Image description" />
            </a>
            <figcaption itemprop="caption description">Image caption  1</figcaption>

            </figure>

        </div>
    </div>

    <div class="col-sm-3 col-md-2 thumb-container" data-image="o3gx9ze57" data-hotlink="9x16er3a7" data-sid="s25">
        <div class="thumb">

<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
  <a href="https://farm2.staticflickr.com/1043/5186867718_06b2e9e551_b.jpg" itemprop="contentUrl" data-size="964x1024">
      <img src="https://farm2.staticflickr.com/1043/5186867718_06b2e9e551_m.jpg" itemprop="thumbnail" alt="Image description" />
  </a>
  <figcaption itemprop="caption description">Image caption 2</figcaption>
</figure>

            </div>

        </div>
</div>

It seems that the issue lies in my JavaScript code, but I'm unsure how to solve it.

// Code snippet for initializing PhotoSwipe from DOM

var initPhotoSwipeFromDOM = function(gallerySelector) {
// Function logic here
};

// Execute the function with selector '.my-gallery'
initPhotoSwipeFromDOM('.my-gallery');

If you have any ideas on how to fix this problem, please let me know. You can view the live demo here.

Answer №1

trying to run the function initPhotoSwipeFromDOM('.thumb'); but it's not working - Last message from Alive to Die 24 minutes ago

@alive to die: issue persists

Answer №2

There is an issue with your hashData on line 198 as it is an empty object without .pid.

The photoswipeParseHash function returns an empty object {} on line 199, resulting in the if statement not being true, which prevents your openPhotoSwipe function from executing.

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

Ways to retrieve directory information in Next.js hosted on Netlify

Having trouble retrieving a list of directories in Next.js on Netlify. The code works fine on localhost, but once deployed to Netlify, an error is triggered: { "errorType": "Runtime.UnhandledPromiseRejection", "errorMessage": ...

The Jquery script seems to be malfunctioning on one specific page while working perfectly fine on

I received a jQuery script from a foreign web page that I'm trying to implement on my own site. $(function () { var $txt = $('input[id$=TextBoxFiltroProv]'); var $ddl = $('select[id$=DropDownListProv]'); var $items = $ ...

Issues with Mouse Hover/Image Replacement

In this particular example, I am attempting to achieve a simple functionality where an image changes when the mouse hovers over it, and reverts back to the original image when the mouse is no longer hovering. However, despite my efforts, there seems to be ...

When initiating the Grunt Express Server, it prompts an issue: Error: ENOENT - the file or directory 'static/test.json' cannot be found

I'm currently in the process of updating my app to utilize the Express Node.js library. As part of this update, I have made changes to my Grunt.js tasks to incorporate the grunt-express-server package. However, after running the server successfully, I ...

React-Native introduces a new container powered by VirtualizedList

Upon updating to react-native 0.61, a plethora of warnings have started appearing: There are VirtualizedLists nested inside plain ScrollViews with the same orientation - it's recommended to avoid this and use another VirtualizedList-backed container ...

Customize your DataGrid filtering with Material UI custom list filter feature

Exploring custom filters in the Material UI DataGrid has been an interesting experience for me. I found some useful information on https://material-ui.com/components/data-grid/filtering/ However, I encountered a challenge when trying to create a list with ...

Unraveling the serialized form data sent via ajax/Jquery and stored in $_POST variables

I am encountering an issue with submitting a form using AJAX and JQuery. serializedData = $form.serialize(); request = $.ajax({ url: "processData.php", type: "post", data: serializedData }); The problem I am facing is that the text becomes ...

Why is the click function being invoked twice, but exclusively on the initial click?

In my current project, I am facing an issue with the onClick action that is being passed down from Context. Strangely, when this action is clicked for the first time, it fires twice. However, from the second click onwards, it functions normally and only fi ...

Determine if the password is accurate using jQuery

Having some trouble with my login form. The JavaScript code always returns a false value, even when I'm entering the correct username and password. This is the jQuery code snippet: $(document).ready(function(){ var teamname = $("#teamname"); var te ...

Parsing error: Unforeseen token encountered. Consider adding a supplementary loader to manage the output of these loaders

Could someone please break down this syntax message?.length === 1 and show me how to convert it into standard JavaScript? https://i.stack.imgur.com/20Ui6.png I am encountering an error when trying to use a Vue.js component that I downloaded from another ...

Is it possible to alter the color of an icon image using CSS?

I'm currently exploring ways to modify the color of an image that has a transparent and solid color split. My goal is to change the white portion for hover effects, and create a dynamic way to alter colors within WordPress without relying on Photosho ...

Troubleshooting problem: AJAX autocomplete URL returning XML

I found my code reference here: http://example.com/code-reference if ($hint=="") { $hint="<a href='" . $z->item(0)->childNodes->item(0)->nodeValue . "' target='_blank'>" . $y->item(0)->childNodes-> ...

What is the method to incorporate spread into inner shadow within an SVG file?

Seeking assistance with creating an inset-shadow effect with spread for an SVG rectangle in Figma. Check out this example of a rectangle with inner-shadow and spread. I have successfully added blur and positioned the shadow using x and y coordinates, but ...

An issue arises when attempting to integrate ajax with colorbox

I am currently facing an issue with a WordPress plugin designed for playing videos. It seems that the developer who created it is no longer available, and now the play video button has stopped working. Upon further investigation using Firebug, I noticed ...

Issues with nested array filtering in JS/Angular causing unexpected outcomes

I am faced with a particular scenario where I need to make three HTTP requests to a REST API. Once the data is loaded, I have to perform post-processing on the client side. Here's what I have: An array of "brands" An array of "materials" An array o ...

Is it possible to include all visible content, even when scrolling, within the full page height?

My webpage contains content that exceeds the height of the window. I am looking for a way to retrieve the full height of my page using either jQuery or pure Javascript. Can anyone provide a solution? I have considered the following approach: $('body ...

If the value of the input matches, set the checkbox to be

I have successfully implemented functionality that allows the value of an input to be changed by clicking a checkbox. This works without any issues. Now, I am facing the challenge of automatically checking the checkbox when the page loads, but only if the ...

Looking to restrict the data retrieved from an API while utilizing ReactJS

I am currently fetching transaction data from an API. One of the fields in the response is buyer, which may sometimes be null. As a result, I am excluding any entries with a null buyer. This leads to varying numbers of results being displayed. My goal is t ...

Merging onsubmit attribute with jQuery submit event listener

My website has a form that is automatically generated by older server-side code. Unfortunately, I am unable to make any modifications to this legacy code for various reasons. The form itself is written in straightforward HTML: <form action="#" id="theF ...

Ensure that clicking on an element closes any currently visible elements before opening a new element

Take a look at my code snippet below. I am working on creating multiple clickable divs that reveal different content when clicked. However, the issue I am facing is that currently, both content blocks can be displayed simultaneously. My goal is to have onl ...