I have a multiple Divs that I want to sort using the JQuery sort function based on the product name attribute inside the product. However, when I click to run the function, all the divs will suddenly disappear, when I inspected to see the errors, there was no error displayed.
Kindly help me know what's the issue.
Here's my JQuery function and call:
<script type="text/javascript">
$(document).ready(function(){
$divs = $("div.section-item");
$("#asc").on("click", function(){
var alphabetically = $divs.sort(function(a, b){
return $(a).find(".name").text() > $(b).find("name").text();
});
$("#section-item").html(alphabetically);
});
});
</script>
This is the container with all the Divs:
<div id="block-section-item">
<div id="bdi-button">
<button type="submit" id="asc" class="rec">Nom</button>
</div>
<div id="section-item">
<?php
include 'Include/__Class/Cls_Product.php';
$product = new ClsProduct($bdd);
$products = $product->DisplayAllProducts();
foreach( $products as $row){
echo'<div class="section-items">
<span class="section-items-img">
<img src="Image/clio-full.jpg">
</span>
<span class="section-items-bottom">
<span class="section-item-text">';
echo '<h4 class="name">'.$row['Product_name'].'</h4>';
echo '<h4 class="price">'.$row['Product_price'].'</h4>';
echo'<span class="section-item-icon">
<span class="voir-item"><a href="'.$_SERVER['PHP_SELF'].'?id='.$row["Product_id"].'"></a></span>
<span class="middle-item"></span>
<span class="nakrih-item"><a href="'.$_SERVER['PHP_SELF'].'?id='.$row["Product_id"].'"></a></span>
</span>
</span>';
echo'</div>';
}
?>
</div>
</div>