I already have a starting code for the adding/removing of value into array. Got from this post - jquery add / remove item from array
My problem is i'm using label, and the jquery code i get isn't using label. I just wanted to get the value of the checkbox or the data-product value inside the label element and add it into the array.
var priceArray = [];
jQuery(document).ready(function($) {
$('.container-box input[type=checkbox]').each(function() {
$(this).change(function() {
if (this.checked) {
priceArray.push($(this).val());
$("#selected-products").html("array=[" + priceArray + "]");
} else {
var index = priceArray.indexOf($(this).val());
if (index > -1) {
priceArray.splice(index, 1);
}
$("#selected-products").html("array=[" + priceArray + "]");
}
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container-box">
<ul class="clearfix">
<li>
<div class="add-checkmark"></div>
<input class="prod-checkbox" name="wagyu_beef[]" type="checkbox" value="4 - 6oz Teres Majors" id="wb1">
<label for="wb1">
<div class="fusion-modal-text-link" title="Click for item details" data-product="4 - 6oz Teres Majors" data-class="wb1" data-toggle="modal" data-target=".fusion-modal.wb1" href="#"><img src="teresmajors.jpg" alt="" class="alignnone size-thumbnail"></div>
</label>
<span>4 - 6oz</span>
<span title="Teres Majors" class="description">Teres Majors</span>
</li>
<li>
<div class="add-checkmark"></div>
<input class="prod-checkbox" name="wagyu_beef[]" type="checkbox" value="4 - 6oz Top Sirloin" id="wb2">
<label for="wb2">
<div class="fusion-modal-text-link" title="Click for item details" data-product="4 - 6oz Top Sirloin" data-class="wb2" data-toggle="modal" data-target=".fusion-modal.wb2" href="#"><img src="topsirloin.jpg" alt="" class="alignnone size-thumbnail"></div>
</label>
<span>4 - 6oz</span>
<span title="Top Sirloin" class="description">Top Sirloin</span>
</li>
<li>
<div class="add-checkmark"></div>
<input class="prod-checkbox" name="wagyu_beef[]" type="checkbox" value="2lbs Tenderloin Tails" id="wb3">
<label for="wb3">
<div class="fusion-modal-text-link" title="Click for item details" data-product="2lbs Tenderloin Tails" data-class="wb3" data-toggle="modal" data-target=".fusion-modal.wb3" href="#"><img src="delicious-grilled-steak-with-seasons-370w.jpg" alt="" class="alignnone size-thumbnail"></div>
</label>
<span>2lbs</span>
<span title="Tenderloin Tails" class="description">Tenderloin Tails</span>
</li>
</ul>
</div>