I’m trying to remove all instances of </div><div class="product-list_row">
and replace it with <!-- removed -->
. But I’m having no luck getting this to work.
I’ve been trying all sorts of solutions, some I couldn’t get at all working, and others would only do the first instance.
I also can’t use a regex solution, since this is on my Shopify website, and their liquid code doesn’t work with regex.
I’ve been using this:
$(document).ready(function(){
$('.product-list').html(function(index,html){
return html.replace('</div><div class="product-list_row">', '<!-- so cool -->');
});
})
.product-list {
display:table;
width:100%;
}
.product-list_row {
display:table-row;
}
.product-list_item {
display:inline-block;
@media screen and (min-width: 1000px) {
display:table-cell;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="product-list">
<div class="product-list_row">
<div class="product-list_item">1</div>
<div class="product-list_item">2</div>
<div class="product-list_item">3</div>
</div>
<div class="product-list_row">
<div class="product-list_item">4</div>
<div class="product-list_item">5</div>
<div class="product-list_item">6</div>
</div>
<div class="product-list_row">
<div class="product-list_item">7</div>
<div class="product-list_item">8</div>
<div class="product-list_item">9</div>
</div>
</div>
Should be this:
.product-list {
display:table;
width:100%;
}
.product-list_row {
display:table-row;
}
.product-list_item {
display:inline-block;
@media screen and (min-width: 1000px) {
display:table-cell;
}
}
<div class="product-list">
<div class="product-list_row">
<div class="product-list_item">1</div>
<div class="product-list_item">2</div>
<div class="product-list_item">3</div>
<!-- removed -->
<div class="product-list_item">4</div>
<div class="product-list_item">5</div>
<div class="product-list_item">6</div>
<!-- removed -->
<div class="product-list_item">7</div>
<div class="product-list_item">8</div>
<div class="product-list_item">9</div>
</div>
</div>
6
Answers
I had to reverse the way I thought about it. Instead of starting with hard coded element wrappers and trying to remove them and replace with another element (comments), instead I should have thought to add the wrapping divs every X elements. This is what I ended up referring to to get what I needed - Wrap and unwrap divs responsively. Thanks for the efforts though!
that is because there are whitespaces between the
</div>
and<div class="product-list_row">
try this instead:
as for the cause of replacing only the first occurrence, please read this helpful document
Using regex is not nearly as reliable as using dom methods
Move the child elements to first row and remove whole containers once emptied
The best way to do this is to use the builtin jquery methods
unwrap
(which removes the parent element) andwrapInner
to add a parent element to the children. Then the javascript simply becomesAdd
id="product"
for theclass="product-list"
. Then you can do: