-1

I would like to target the following attribute value: data-search-position

<div id="result-item-1" class="search-item " data-search-position="1">
    <div class='item-colours-result'>
        <ul class='list-class'><li>1</li><ul>
  </div>
</div>

<div id="result-item-2" class="search-item " data-search-position="2">
    <div class='item-colours-result'>
        <ul class='list-class'><li>1</li><ul>
  </div>
</div>

As the data-search-position will define how many list items it will have, I need to find out how to do that. I have the following so far but it's no good:

$(document).ready(function () {

    var colorCount = $('#result-item-1 .item-colours-result ul li');
    if (colorCount.length > 0) {
        // do something
    }

    else {
        //do something
    }

});
freginold
  • 3,946
  • 3
  • 13
  • 28
Sole
  • 3,100
  • 14
  • 58
  • 112

1 Answers1

1

You can use a loop over .search_item instead of specific #id, and then apply your logic. When you get the list count, set the attribute with .attr()

$('.search-item').each(function(index,item){
  var colorCount = $(item).find('.item-colours-result ul li').length;
  // alert(item.id+':'+colorCount) count by item id
  $(item).attr('data-search-position',colorCount)
})

See this jsfiddle:

https://jsfiddle.net/bh8gL15f/

F.Igor
  • 4,119
  • 1
  • 18
  • 26