I am currently attempting to filter my product list using jQuery based on price (low/high). I have managed to implement this functionality successfully. However, I am facing an issue where clicking the filter causes my products to lose their formatting and ignore their bootstrap classes. I'm unsure why this is happening.
Here is an example of the code:
If you click the link below, you will see the problem in action. Any suggestions or ideas on how to resolve this?
var ascending = false;
$('.tab-content').on('click', '.sortByPrice', function() {
var sorted = $('.results-row').sort(function(a, b) {
return (ascending ==
(convertToNumber($(a).find('.price').html()) <
convertToNumber($(b).find('.price').html()))) ? 1 : -1;
});
ascending = ascending ? false : true;
$('.results').html(sorted);
});
var convertToNumber = function(value) {
return parseFloat(value.replace('$', ''));
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<div class="tab-content">
<div id="filters">
<p>
<a class="sortByPrice" href="#">Sort by Price</a>
</p>
</div>
</div>
<div class="results">
...
...
</div>
<!--container.//-->