Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Carousel not working if the code is added dynamically by jQuery

I've a carousel on my website, that works perfectly on hardcoding it. This is what the code looks like:

<div class="w-full flex justify-center">
   <div class="gallery js-flickity"
       data-flickity-options='{ "wrapAround": true }'
   >
     <div class="gallery-cell">
       <div class="w-full flex justify-center">
           <img src="./img/sidenav-1/clients-01.png" class="client-image"/>
       </div>
     </div>
     <div class="gallery-cell">
       <div class="w-full flex justify-center">
          <img src="./img/sidenav-1/clients-02.jpeg" class="client-image" />
       </div>
     </div>                  
 </div>

I've to use this carousel due to certain coding restricions I've so I can't change it. Now I have to connect this webpage to an admin panel, in which I'm using PHP for fetching the values from the database and echoing the same entire code and usind jQuery to put it in place. But If I do so instead of a carousel the images are displayed one beneath the other.

jQuery Code:

<script>
  $.ajax({url: "API/clients.php", success: function(result){
     $("#append_clients").html(result);
  }});
</script>

PHP:

$resultee = '<div class="gallery js-flickity" data-flickity-options=\'{ "wrapAround": true }\'>';

$sql = "SELECT * FROM Clients;";
$result = $conn->query($sql);

$conn->close();

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
      $image_path = $row["image_address"];
      $client_name = $row["client_name"];
      $client_desc = $row["client_desc"];
    $resultee .= '<div class="gallery-cell"><div class="w-full flex justify-center"><img src="'.$image_path.'" class="client-image" /></div></div>';
  }
} 

$resultee .= '</div>';

CSS and JS:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flickity/1.0.0/flickity.css" />
<style>
.gallery {
  width: 93vw;
  background: #e5e5e5;
  margin-left: -5%;
}

.gallery-cell {
  width: 93vw;
  margin-right: 10px;
}
    
   
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flickity/1.0.0/flickity.pkgd.js"><script>

Can anyone please help, I'm unsure what to do

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I had a similar issue with a dynamically loaded flickity carousel where all of my cells were initially stacked on top of each other. It could be two things: you're just adding elements to the html instead of using flickity 'append' to add items to the initialized carousel. Second, if you call 'resize' after on a visible and populated carousel, it should be able to measure and position the elements correctly.

/* init carousel via jquery */
var $carousel = $('.gallery').flickity( { "wrapAround": true } );

/* add item(s) to $carousel */
$carousel.flickity( 'append', your_item);

/* force a redraw */
$carousel.flickity('resize');

API for reference: https://www.tszshan.org/home/new/common/js/flickity-docs/api.html#resize

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!