I'm trying to dynamically add items to an Owl carousel. Here is how I'm doing it:
HTML
<div id="avatar-carousel" class="owl-carousel lesson-carousel">
<div class="item item-logo">
<div class="product-item">
<div class="carousel-thumb" style="height: 77px!important;width: 70px;" >
<img src="http://placehold.it/140x100" alt="">
</div>
</div>
</div>
<!-- Start Item -->
</div>
<button id="click">
click
</button>
JS
$("#avatar-carousel").owlCarousel({
items: 5
});
$("#click").click(function(){
$('#avatar-carousel').trigger('add.owl.carousel', ['<div class="item"><p>' + 'hh' + '</p></div>'])
.trigger('refresh.owl.carousel');
});
Nothing happens with this code. I can't see the error. Here is the fiddle: JSFiddle.
EDIT:
Seems like the code is correct, as it's working in the fiddle. I forgot to mention that the carousel works just fine, it is loaded correctly in the beginning. The issue is when trying to add items. Nothing happens, no errors but items are not added.
It seems that all answers here work perfectly, but not in my case. Probably the events that are called are in conflict with others. So I did it another way:
Clear the content of the carousel
Append the new content into the carousel
Where imgs is an array of image urls, and createImgCarousel a function that creates a carousel item.
Finally reinitialize the carousel
It's maybe not so clean, but it works for now !
There can be two regular errors:
onclick
event.<form>
and clicking the button submits the entire form (pay attention at browser page loading). Error shown here.