I've got a list of divs with the same class that contain brand names, I've been able to get it to run through the divs and store all the brand names (text) in an array with the below:
var brandList = $('.brand').map(function(){
return $(this).text();
}).get();
console.log(brandList);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="brand">Brand 1</div>
<div class="brand">Brand 2</div>
<div class="brand">Brand 3</div>
I then have a product feed on the same page pulling in products however the brand name and product name are part of the same feed field and I need to separate them on the front end to style them up different.
.productListItem {
display: inline-block;
width:49%;
text-align: center;
}
.productListItem img {
width:100%;
}
<div class="productListItem">
<img src="https://assets.pernod-ricard.com/nz/media_images/test.jpg?hUV74FvXQrWUBk1P2.fBvzoBUmjZ1wct" />
<div class="name">Brand 1 Product 1</div>
</div>
<div class="productListItem">
<img src="https://assets.pernod-ricard.com/nz/media_images/test.jpg?hUV74FvXQrWUBk1P2.fBvzoBUmjZ1wct" />
<div class="name">Brand 2 Product 2</div>
</div>
<div class="productListItem">
<img src="https://assets.pernod-ricard.com/nz/media_images/test.jpg?hUV74FvXQrWUBk1P2.fBvzoBUmjZ1wct" />
<div class="name">Brand 3 Product 3</div>
</div>
I've been doing this with a simple script manually entering each brand text I wanted to isolate (which works fine):
$('.productListItem .name').html(function (index, text) {
this.innerHTML = text.replace("Brand 1 ", "<div class='brandName'>Brand 1 </div>");
});
However being manual things can be easily missed and I want to keep it all JQuery for site consistency. Seems as the brand list exists on the page and I've managed to store them in an array I want to automate it so for each product on the page it looks at the product name div then loops through the array, and if a brand name in the array matches part of the product name it will isolate the matching brand name in the product name by wrapping a div around it (so I can style it up different).
I've tried to figure this out but I'm still learning and this seems past my knowledge base currently as I haven't even got close (past storing the brands in the array), I've also tried searching for the answer but not had the greatest luck finding something I can understand and adapt to work.
Many thanks in advance