The script fails to function when called from within the code, yet it operates perfectly when manually typed into the console

Incorporating the flexslider widget for displaying a photo gallery has been quite straightforward. The markup is simple and adjusts seamlessly to the viewport upon document load, with styling applied once the document is ready. Initially, everything functions as expected when the page loads. However, there arises a need for a "selector" feature where clicking on a link triggers the loading of a new gallery within the flexslider widget.

To achieve this, I utilized jQuery to select the corresponding gallery div and update its contents using `$("#galleryID").empty().append(new-markup);`. While this updates the DOM structure accordingly, the issue arises when the flexslider fails to display the new content due to the necessity of running the script that adapts it to the viewport and styles it. Despite attempts to rerun the necessary script post-modification, the div remains empty. Interestingly, invoking the same script from the console results in the correct display of the div.

My current approach involves utilizing the $.getScript() method in jQuery to reload the adaptation and styling script. Yet, this solution is inconsistent - sometimes it works, but other times it does not. Surprisingly, Firefox consistently executes the scripts and displays the gallery correctly upon selecting the selector. On the other hand, Chrome proves unreliable, occasionally rendering the div blank despite having the correct markup sans the required modifications.

<script type='text/javascript'>
    $('a.pgal').click(function () {
        var id = $(this).attr('hashtag');
        /*fetch gallery elements (images, titles, etc)*/
        $.get("listgal.php", {hashtag:id}, function(objetogal){
            // code snippet
        }, "json");
        $.getScript("js/renueva.js", function(){
        });
        return false;
    });
</script>

The additional script 'renueva.js' encompasses the essential code responsible for styling and adapting the gallery to the viewport using the provided markup source. Previous attempts to include this code within the same script post-refresh proved futile, as it failed to produce any visible changes. Performing the same action via the console yielded positive results. Presently, experimenting with $.getScript exhibits varying outcomes – at times, processing the markup successfully, while at others failing to do so when using Chrome. In contrast, Firefox consistently executes without issues.

// Flexslider initialization
$('#carousel').flexslider({
    animation: "slide",
    controlNav: false,
    animationLoop: false,
    slideshow: false,
    itemWidth: 90,
    itemMargin: 5,
    asNavFor: '#slider'
});
$('#slider').flexslider({
    animation: "slide",
    controlNav: false,
    animationLoop: false,
    slideshow: false,
    sync: "#carousel",
    start: function(slider){
    }
});

Below is the HTML markup:

<div class="col-xs-12 w12 col-md-9" id="galeriabr">
    <div id="slider" class="flexslider">
        <ul class="slides">
            <li>
                <img src="fotos/armas.jpg" />
                <p class="flex-caption">Se presentan 98 delitos al día en Michoacán</p>
            </li>
            <li>
                <img src="fotos/sep.jpg" />
                <p class="flex-caption">Se tomarán medidas para garantizar evaluación a maestros: SEP</p>
            </li>
            <li>
                <img src="fotos/eleccion.jpg" />
                <p class="flex-caption">Scioli gana elección de Argentina pero puede haber segunda vuelta</p>
            </li>
        </ul>
    </div>
    <div id="carousel" class="flexslider">
        <ul class="slides">
            <li>
                <img src="fotos/armas.jpg" />
            </li>
            <li>
                <img src="fotos/sep.jpg" />
            </li>
            <li>
                <img src="fotos/eleccion.jpg" />
            </li>
        </ul>
    </div>              
</div>

Answer №1

An error was discovered in the loop where the markup is generated, as the < /li> tag was missing. Although it functions better now, there are still instances where the empty div reappears. I made an adjustment to the $.getScript() line:

    $.when(
    $.getScript( "js/renueva.js" ),
    $.Deferred(function( deferred ){
    $( deferred.resolve );
     })
    ).done(function(){
      recarga();
     });

The "recarga()" function is located within "renueva.js." I will continue exploring alternative solutions because it is not yet flawless. My goal is for the gallery to display every time its selector is clicked, rather than 9 out of 10 clicks.

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

Troubleshooting Issue: Incompatibility with CSS Webresources in Custom ASP Control Library

In my latest project, I created a Custom Control DLL that extends existing Telerik and ASP controls. As part of this process, I also introduced new CSS classes that utilize images. However, I encountered an issue where these images were not being loaded in ...

The feature of Jquery click and cursor: pointer only focuses on a single element at a

Recently, I created a navbar that includes a dropdown feature. I encountered an issue where I wanted the dropdown to show up when clicking on the navbar-profile class (which contains the profile picture, name, and chevron icon) in the top right corner. Ho ...

Is there a way to extract the text that lies between two closed HTML

Looking for a solution using jQuery. <pre><marker id="markerStart"></marker> aaaaa <span style='font-family:monospace;background-color:#a0a0a0;'>bbb</span>bb cc<marker id="markerEnd"></marker>ccc </pr ...

What are the best Illustrator settings for exporting SVGs for the web?

Seeking to implement a responsive SVG logo on a website that looks great across all devices is my current goal. Considering the issues surrounding SVG usage, I aim to ensure broad device and browser support while also prioritizing loading speed. How do Ad ...

What is the best way to insert an image below my Bootstrap and Angular design?

I currently have the backgroundlogin.jpg image stored in the assets directory of my Angular project. I'm interested in learning how to position it below my layout, specifically in the white section indicated by the arrows in the figure provided below: ...

Eliminate repetitive content in CSS document

I recently discovered the SCCS Compiler () and I am impressed with its capabilities. However, I am in need of an additional feature. After generating a compressed CSS file, I noticed the following: html,body{height:100%;}html,body{height:100%;}html,body{ ...

Generate an adjustable grid layout (with rows and columns) to display information retrieved from an API request

I have recently started learning React JS and JavaScript. To practice, I am working on a small project where I am facing an issue with creating dynamic rows and columns. My aim is to display data in 4 columns initially and then move to a new row and column ...

The reply from the Servlet webpage is displayed on a fresh page

I am currently using Ajax and jQuery to handle the dynamic form submission process. While everything is functioning correctly, I am encountering an issue where the response from the Servlet is being displayed on a separate page. function initiateFormSubm ...

"Retrieving a unique identifier from a child page by using the href attribute in the parent page

Is it possible to load a specific ID from a child page and display that content in a dialog on the parent page when a link is clicked? I want to load the DIV ID "medical" when the medical link is clicked, and I am looking for a way to add a selector in the ...

Tips for styling the four DIV elements on a website's homepage with CSS

In my current project, I am working on a webpage that requires specific layout elements. On the web homepage, I need to split it into four sections positioned between the header and footer. The main code snippet for the homepage structure is as follows: ...

Error in retrieving image from database

I am currently working on a file upload and download feature using ajaxFileUploader along with 2 handlers. Here is the ajaxFileUploader code snippet: $.ajaxFileUpload ( { url: 'AjaxFileUploader.ashx?user=' + userId, ...

Using CSS to repeat an image

Is there a way to set cap insets when defining the background-image repeat? I need the image to repeat without borders. Can CSS allow for tiling or stretching the middle of the image? The first rounded rectangle represents my PNG image with defined cap in ...

Stack bind error encountered

Has anyone encountered a javascript error that they can't seem to figure out? I'm getting the following error message: $("input[type=text],input[type=password],input[type=checkbox],input[type=radio],select,textarea").bindIntoStack is not a funct ...

Refresh the options in a dropdown menu after selecting a radio button using jQuery

Upon selecting either the male or female radio button, I wish to populate a list with names corresponding to the selected gender from a database. The male and female tables in the database consist of two columns: {id} and {name}. <?php include "co ...

The puzzling Webkit shadow inset glitch

This particular issue differs from the Google Chrome inset box-shadow bug on Windows, not on Mac: Better workaround? However, it appears to be connected to the problem discussed in: Weird box-shadow artifacts in webkit. When applying an inset box-shadow ...

Tips for eliminating the small amount of horizontal scrolling on mobile devices when utilizing viewport width=device-width

My issue involves using the meta tag to set the viewport width to match the device's width: <meta(name='viewport', content='width=device-width, initial-scale=1.0, user-scalable=0;') However, upon loading the page on Chrome for ...

Creating a combination of associative keys and numbers within an array using JavaScript

To summarize, my question starts below: I have simply read a JSON file with the following contents: [{"FirstCategory":"Bath","SecondCategory":"Bath Accessories","ThirdCategory":""}, {"FirstCategory":"Bath","SecondCategory":"Faucets","ThirdCategory":""}, ...

Is there a JQuery image viewer that can be identified by its id?

Is it possible to use iviewer with an image id instead of the image source? <script type="text/javascript"> var $ = jQuery; $(document).ready(function(){ var iv1 = $("#viewer").iviewer({ src: "test_image.jpg", --- ...

having numerous collapsible elements that trigger a single django view function, each one submitting a distinct value

I have multiple collapsible items that need to trigger the same Django function when submitted with different values. https://i.sstatic.net/1BO19.png Is there a way to achieve this without using a dropdown menu in Django forms? What modifications should ...

Using CKEditor in an AngularJS web application: Tips and tricks

I'm having trouble integrating the ckeditor into an HTML page that's built with angularjs. Despite trying out numerous examples, such as the ng-ckeditor and ckeditor directives, I haven't found a solution that works for me. What I need is ...