adit
adit

Reputation: 33644

how to find li with a particular data

I have the following html code:

<ul class="dropdown-menu shopping-cart-nav-drpdown">                                                              
    <li class="item" data-item-id="58">

     </li>
     <li class="item" data-item-id="100">

     </li>
     <li class="item last">
     <a href="/cart" class="btn btn-info full-button">View Cart</a>
     </li>
</ul>

Inside this shopping-cart-nav-drpdown, how do I find the li that has data-item-id=58? Is it possible to do this without iterating through all the children of ul and use getAttributes?

Upvotes: 0

Views: 127

Answers (3)

FLF
FLF

Reputation: 157

try this

jQuery( ".shopping-cart-nav-drpdown li" ).each(function(){
    if($(this).data('item-id')=="58"){
    alert('found');
    }
  });

Upvotes: -1

palaѕн
palaѕн

Reputation: 73906

Yes, it is possible to do this without iterating through all the children of ul:

$('.shopping-cart-nav-drpdown li[data-item-id="58"]');

FIDDLE DEMO #1

You can also make it dynamic like this:

var data = 58;
$('.shopping-cart-nav-drpdown li[data-item-id="' + data + '"]');

FIDDLE DEMO #2

Upvotes: 2

mishik
mishik

Reputation: 10003

Sure:

 $("li[data-item-id=58]")

And an awesome Demo

Upvotes: 5

Related Questions