The previous button on Owl Carousel seems to be malfunctioning after navigating from a different page

In my case, I have two distinct pages: let's call them the first and second pages.

On the first page, I utilize an owl carousel slider with a tag that links to a specific slide on the second page's owl carousel slider using an ID. Meanwhile, on the second page, there are previous and next buttons that change names each time the user switches slides to guide them. Everything seems to work fine until the user navigates from the first page to the second one and attempts to click the previous button right away, which doesn't function correctly. However, if they click the next button first and then the previous one, it works as intended. Here are the relevant code snippets:

FIRST PAGE

<div class="owl-carousel home-page-project-design home-page-manufacturing-list">
        <a href="../project-design-showcase.php#walk-in-wardrobes">
            ...
            content goes here
            ...
        </a>
        ... more similar layout for other sections ...
    </div>
    

SECOND PAGE

<section class="next-and-previous-title-showcase">
        ...
        additional details displayed in this section can be found here
        ...
    </section>

    <div class="owl-carousel test3">
        ...
        include corresponding files for different carousel slides
        ...
    </div>

    <script>
        ...
        jQuery script inside this section controlling the functionality of the carousel
        ...
    </script>
    

Answer №1

SOLUTION

<script>
$(document).ready(function() {
    var fragmentMap = {
        'custom-closets': 0,
        'living-rooms': 1,
        'dining-areas': 2,
        'home-offices': 3,
        'entertainment-centers': 4,
        'wine-cellars': 5,
        'patios': 6
    };

    var owl = $('.test3').owlCarousel({
        stagePadding: 10,
        responsive: {
            0: {
                items: 1
            },
            768: {
                items: 1
            },
            1280: {
                items: 1
            }
        },
        touchDrag: true,
        mouseDrag: false
    });

    function updateButtonLabels(currentItem) {
        $('.prev-button').text(prevButtonLabels[currentItem]);
        $('.next-button').text(nextButtonLabels[currentItem]);
    }

    var nextButtonLabels = [
        'Living Rooms',
        'Dining Areas',
        'Home Offices',
        'Entertainment Centers',
        'Wine Cellars',
        'Patios',
        ''
    ];

    var prevButtonLabels = [
        '',
        'Custom Closets',
        'Living Rooms',
        'Dining Areas',
        'Home Offices',
        'Entertainment Centers',
        'Wine Cellars'
    ];

    var currentItem = 0;

    // Get the fragment identifier from the URL
    var fragment = window.location.hash.substring(1);

    if (fragmentMap.hasOwnProperty(fragment)) {
        currentItem = fragmentMap[fragment];
    }

    // Initialize the Owl Carousel with the currentItem
    owl.trigger('to.owl.carousel', [currentItem, 0, true]);
    updateButtonLabels(currentItem);

    $('.prev-button').click(function() {
        if (currentItem > 0) {
            owl.trigger('prev.owl.carousel');
            currentItem--;
            updateButtonLabels(currentItem);
        }
    });

    $('.next-button').click(function() {
        if (currentItem < prevButtonLabels.length - 1) {
            owl.trigger('next.owl.carousel');
            currentItem++;
            updateButtonLabels(currentItem);
        }
    });
});
</script>

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

In the realm of asp.net mvc, JSON remains a mysterious and undefined

When working with ASP.NET MVC 3, I have encountered an issue regarding JSON in my AJAX calls. The application runs smoothly on my development machine when using Visual Studio. However, after publishing the same application and trying to access it through a ...

Is it possible to load asynchronous JS and then execute functions?

Is there a way to make my script behave like the Google Analytics JavaScript snippet? Here is an example of what I have: (function(d, t) { var g = d.createElement(t), s = d.getElementsByTagName(t)[0]; g.src = 'myjs.js'; s.par ...

Efficient State Management with React-Redux for Streamlined HTTP Requests

In the process of developing a React-Redux application, I have encountered a situation where I need to display a cancel button while a specific HTTP request is ongoing. Upon successful execution of the request, the UI should display the results. If the use ...

Encountered an error while trying to download a PDF document in React

I'm currently working on adding a button to my website portfolio that allows users to download my CV when clicked. The functionality works perfectly fine on my localhost, but after deploying it to AWS Amplify, I encountered an error. The error occurs ...

Is anyone else experiencing issues with their imagemap due to Twitter Bootstrap?

I'm excited to ask my first question here as a beginner. I am truly grateful for all the assistance and guidance I have received from this site. I hope that the questions I ask can also benefit others in some way :) Although imagemaps may not be used ...

AJAX Image Upload: How to Transfer File Name to Server?

Has anyone successfully uploaded an image to a web server using AJAX, but struggled with passing the file name and path to the PHP script on the server side? Here is the HTML code along with the JavaScript (ImageUpload01.php) that triggers the PHP: Pleas ...

Is it possible to perform a legitimate POST request using AJAX XMLHttpRequest, rather than the traditional var=val URL type post?

Apologies for the unclear question, but here is my query... I recently began using Ajax and encountered an issue with sending XMLHttpRequest in the background. I am facing problems with certain html special characters in the form data, especially the & ...

The meaning gets blurred when using 'this' in a prototype method

Alright, so I am working on a node application that utilizes express. In this particular application, there's an express route set up like this: var MyObject = require('./myObject') , myObject = new MyObject(); app.post('/api/w ...

What can be done to address the problem of background-image opacity making text difficult to read?

I am having an issue with the background. I am aiming for something similar to what is shown in screenshot 1, but instead, I am getting the result shown in screenshot 2. Where could the problem lie? html, body, .bg-for-1 { height: 100%; } .bg-for-1 ...

Update the URL of the entire window by changing the link within the frame

Hey everyone, just a heads up - I haven't included frames in my programming for this page. They'll be removed soon. But right now, I have to figure out how to tackle this issue. The challenge at hand is that there's a link inside a frame. I ...

Leveraging API data within React: A step-by-step guide

I have a React application where I am making API calls. To manage these calls, I created a separate file called dataService.js to contain all the functions for interacting with the API. However, when trying to log the data in my component, I am getting a ...

Showing data from the POST request in a dialog box

Greetings to all! I am in the process of building a webpage to test my API. The goal is to send a form to my nodejs backend and receive a JSON response back. So far, sending the request is functioning properly and I can track its progress on my VPS conso ...

The function this.someFunction does not exist

Even though I have gone through the details of the bind requirement for methods to be associated with a React ES6 class, I am still facing challenges with this specific example: class ProductList extends React.Component { constructor(props) { super( ...

The essential guide to creating a top-notch design system with Material UI

Our company is currently focusing on developing our design system as a package that can be easily installed in multiple projects. While the process of building the package is successful, we are facing an issue once it is installed and something is imported ...

Uncaught jQuery onchange event not firing

I am facing an issue with a drop-down list inside a data grid on the main page. When values are changed, a popup should display and the values from the popup need to be sent back to the main page. However, I am having trouble triggering the onchange event. ...

Stop the form submission from redirecting or refreshing by utilizing JavaScript

I'm facing an issue with a page on my website that includes textboxes generated from php, along with two buttons - GetData and SaveData. My goal is to enable users to press the enter key while editing a textbox and trigger the same action as clicking ...

What is the best method for returning the AJAX outcome to the JSP page?

Using AJAX, I am able to post data from my JSP page to my servlet. $.ajax({ url: 'myServlet?action=FEP', type: 'post', data: {machine: i, name: txt}, // where i and txt hold certain values success: f ...

What is the method for applying a stroke-width:1 to specific sides of SVG shapes?

When setting a stroke-width: 1 on a <rect> element in SVG, typically a stroke is placed on every side of the rectangle. Is there a way to specify a stroke width on just three sides of an SVG rectangle? ...

The table does not recognize any of the CSS classes when the TAG is inputted inside

I am currently working on a challenging form for players to save their personal character sheets in a database. To achieve this, I am incorporating numerous Input TAGs and utilizing tables for layout purposes, ensuring an organized and efficient design. E ...

Enhancing CSS and HTML: Increasing the Border Color of Dropdown Menus and Utilizing Multiple Words per Dropdown Item

CODE div.box { display: block; text-align: center; margin: auto; z-index: 5 } #navMenu { margin: 0; padding: 0; } #navMenu ul { margin: 0; padding: 0; line-height: 30px; } #navMenu li { margin: 0; padding: 0; list-style: none; float: left; positio ...