0

I am fetching data from firestore database to create a product card so that I can display the product cards inside a carousel. Owl carousel to be specific. I am using the innerHTML property in javascript to add the product cards programmatically. The Owl carousel seems to not work when I do so but when I create the cards statically in html the carousel works. What could be the problem?

Code:

Html:

    <div class="product-area most-popular section">
        <div class="container">
            <div class="row">
                <div class="col-12">
                    <div class="section-title">
                        <h2>Hot Items</h2>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-12">
                    <div class="owl-carousel popular-slider" id="owlCarousel">

                     <!--product cards go here-->

                    </div>
                </div>
            </div>
      </div>
  </div>

Javascript:

var view2 = document.getElementById("owlCarousel");

db.collection("products").where("hot" , "==" , true)
.get().then((querySnapshot)=> {
    querySnapshot.forEach((doc) => {

        var image = doc.data().images[0];
        var image2 = doc.data().images[1];
        var title = doc.data().title;
        var price = doc.data().pricing.toString();
        var hot = doc.data().hot;
        var discount = doc.data().discount;
        var price = doc.data().pricing;
            
           
         //product card creation

            view2.innerHTML += `

            <div class="single-product">
            <div class="product-img">
                <a href="product-details.html">
                    <img class="default-img" src="${image}" alt="#">
                    <img class="hover-img" src="${image2}" alt="#">
                    <span class="out-of-stock">Hot</span>
                </a>
                <div class="button-head">
                    <div class="product-action">
                        <a data-toggle="modal" data-target="#exampleModal" title="Quick View" href="#"><i class=" ti-eye"></i><span>Quick Shop</span></a>
                        <a title="Wishlist" href="#"><i class=" ti-heart "></i><span>Add to Wishlist</span></a>
                        <a title="Compare" href="#"><i class="ti-bar-chart-alt"></i><span>Add to Compare</span></a>
                    </div>
                    <div class="product-action-2">
                        <a title="Add to cart" href="#">Add to cart</a>
                    </div>
                </div>
            </div>
            <div class="product-content">
                <h3><a href="product-details.html">${title}</a></h3>
                <div class="product-price">
                    <span>Ksh. ${price}</span>
                </div>
            </div>
        </div>

        `


    });

});

1 Answers1

0

I just had the same problem. What you should do is initialize the OWL CAROUSEL after you add the template (inner html);

for example:

    view.innerHtml = `<div> content </div`;
    $('.owl-carousel').owlCarousel({
        //here you can add the props or events
    })

This worked for me! hope this could hel you :)