I've succeeded in implementing most of the desired functionality via this JavaScript but there is still a potentially troublesome bug that I can't figure out: the code I wrote appends desired HTML but creates unnecessary duplicates.
The Problem
The code appends the button multiple times, when it should append just once. How do I stop the multiples?
The (li)st items show up in a ul styled like a table like this (the numbering is arbitrary and I only use it for our reference):
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
- It gives listing 4, 1 buttons
- It gives listing 1, 2 buttons
- It gives listing 2, 3 buttons
Is it going right-to-left top-to-bottom? And if so, why?
What I tried to accomplish:
Stage 1 - Check for video and add button if video present
- Check product listings on this category page to see if any have ".Options"
- If they have .Options, check to see if a video matching this product's ID exists in /Videos/ folder
- If video with matching Product ID exists in videos folder, attach a Play/Stop toggle button to product listing
Stage 2 - Play/Stop video on button press
- If Play/Stop button is pressed, add tag with relevant video URL
- If video is playing, stop and hide video upon button press
The JavaScript
$('.videoDemoBtn').on('click', function () {
if ($(this).hasClass('videoPlaying')) {
$(this).removeClass('videoPlaying');
$(this).parent().find('div.categoryDemoVideo').hide().html('');
}
else {
var ProductId = $(this).parent().find('div.ProductImage').attr('data-product');
$(this).addClass('videoPlaying');
$(this).parent().find('div.categoryDemoVideo').show().html('<video id="demoVideo" class="video" preload="auto" autoplay="autoplay" loop="loop" autobuffer="autobuffer" muted="muted" controls="controls" width="100%" height="100%" poster="https://store-mixi7d.mybigcommerce.com/content/videos/'+ProductId+'.jpg"><source src="https://store-mixi7d.mybigcommerce.com/content/videos/'+ProductId+'.mp4"><source src="https://store-mixi7d.mybigcommerce.com/content/videos/'+ProductId+'.ogv" type="video/ogg"><p>Your browser does not support this video. Please upgrade your browser!</p></video>');
}
});
$(".Options").each(function checkForVideo(url) {
var ProductCatOpt = $(this);
ProductId = $(this).parent().parent().find('div.ProductImage').attr('data-product');
function ajax1() {
return $.ajax('/content/videos/'+ProductId+'.mp4')
.done(function() {
$(ProductCatOpt).addClass('withVideo');
}).fail(function() {
return;
});
}
$.when(ajax1()).done(function(a1){
$('.withVideo').closest('li').append('<span class="videoDemoBtn"><div class="triangle"></div></span>');
});
});
The HTML
<ul class="ProductList " style="position: relative; height: 1407px;">
<li class="Odd " style="min-height: 456px; position: absolute; left: 0px; top: 0px;">
<div class="ProductImage QuickView" data-product="296">
<div class="categoryDemoVideo"></div>
</div>
<div class="ProductActionAdd" style="display:;">
<a href="#someproduct" class="btn Small icon-Choose Options withVideo" title="Choose Options">Choose Options</a></div>
<!-- Here in the first listing in the ul with class .Options it creates 2 buttons -->
<span class="videoDemoBtn"><div class="triangle"></div></span>
<span class="videoDemoBtn"><div class="triangle"></div></span>
<!-- Here in the first listing in the ul with class .Options it creates 2 buttons -->
</li>
<li class="Even " style="min-height: 456px; position: absolute; left: 310px; top: 0px;">
<div class="ProductImage QuickView" data-product="431">
<div class="categoryDemoVideo"></div>
</div>
<div class="ProductActionAdd" style="display:;">
<a href="#someproduct" class="btn Small icon-Choose Options withVideo" title="Choose Options">Choose Options</a></div>
<!-- While here in the 2nd to last listing in the ul with class .Options it creates 3 buttons -->
<span class="videoDemoBtn"><div class="triangle"></div></span>
<span class="videoDemoBtn"><div class="triangle"></div></span>
<span class="videoDemoBtn"><div class="triangle"></div></span>
<!-- While here in the 2nd to last listing in the ul with class .Options it creates 3 buttons -->
</li>
<li class="Odd " style="min-height: 435px; position: absolute; left: 0px; top: 476px;">
<div class="ProductImage QuickView" data-product="389">
<div class="categoryDemoVideo"></div>
</div>
<div class="ProductActionAdd" style="display:;">
<a href="#someproduct" class="btn Small icon-Add To Cart" title="Add To Cart">Add To Cart</a></div>
</li>
<li class="Even " style="min-height: 435px; position: absolute; left: 310px; top: 476px;">
<div class="ProductImage QuickView" data-product="393">
<div class="categoryDemoVideo"></div>
</div>
<div class="ProductActionAdd" style="display:;">
<a href="#someproduct" class="btn Small icon-Choose Options withVideo" title="Choose Options">Choose Options</a></div>
<!-- And yet ere in the last listing in the ul with class .Options it creates just 1 button -->
<span class="videoDemoBtn"><div class="triangle"></div></span>
<!-- And yet ere in the last listing in the ul with class .Options it creates just 1 button -->
</li>
<li class="Odd " style="min-height: 456px; position: absolute; left: 0px; top: 931px;">
<div class="ProductImage QuickView" data-product="428">
<div class="categoryDemoVideo"></div>
</div>
<div class="ProductActionAdd" style="display:;">
<a href="#someproduct" class="btn Small icon-Add To Cart" title="Add To Cart">Add To Cart</a></div>
</li>
<li class="Even " style="min-height: 456px; position: absolute; left: 310px; top: 931px;">
<div class="ProductImage QuickView" data-product="388">
<div class="categoryDemoVideo"></div>
</div>
<div class="ProductActionAdd" style="display:;">
<a href="#someproduct" class="btn Small icon-Add To Cart" title="Add To Cart">Add To Cart</a></div>
</li>
</ul>
I've done a JSFiddle here but it's of no help as the videos that must be checked for with ajax have to be on same domain for this to work, and the ProductIDs are dynamically generated by the PHP of the platform I am using.
I am specifically looking to stop the behavior that causes multiple buttons to be appended instead of just 1, however, I wouldn't be surprised if my code was sloppy and inefficient so please feel free to criticize any part of this.