Thanks a million for taking the time to read this :) First let me show you the example code I am dealing with:
<div id="section-0000001" class="section" data-section-223344="{"id":"123456", "type":"product"}">
<div>Section 0000001</div>
</div>
<div id="section-0000002" class="section" data-section-223344="{"id":"123456", "type":"category"}">
<div>Section 0000002</div>
</div>
<div id="section-123456" class="section" data-section-223344="{"id":"123456", "type":"showcase-product"}">
<div>Section 123456</div>
</div>
<div id="section-234567" class="section" data-section-223344="{"id":"234567", "type":"showcase-product"}">
<div>Section 234567</div>
</div>
<div id="section-345678" class="section" data-section-223344="{"id":"345678", "type":"showcase-product"}">
<div>Section 345678</div>
</div>
<div id="section-0000003" class="section" data-section-223344="{"id":"123456", "type":"image"}">
<div>Section 0000003</div>
</div>
What I want to achieve, is to add a class to all divs with the type 'showcase-product'. I do not have access to the original code so I need to do this with Jquery. There is unfortunately not a unique class to filter these out and the only thing unique is the showcase-product but as you can see that is part of an object within the data attribute and I cannot figure out how to access it.
If the attribute data-section-xxxxxx was the same, this would be much easier but each div has a unique value on the end but the first part data-section- is always the same.
I can loop through the divs with this:
$('.section').each(function(){
for(data in $(this).data())
console.log(data);
});
But I cannot figure out a way to filter out only the divs with type = showcase-product.
I also tried a different approach:
$( ".section" ).each(function( i ) {
element=this;
$.each(this.attributes, function() {
if(this.name.indexOf('data-section') != -1)
$(element).addClass("myClass");
});
});
This adds the class but it also adds it to the ones without type = showcase-product such as type = image or type = category.
I guess it might be something like
this.name.type.value.indeOf('data-section') != 1
But I am not sure of the correct syntax or how to access it especially since type is not the value, it's an object within the value.
Any help appreciated.