Vladimir Potapov
Vladimir Potapov

Reputation: 2437

How to find an element based on a two data-attribute value?

 <ul>
    <li id="RadListBox1_i2" class="rlbItem ui-draggable">

        <div class="ui-draggable ui-state-default" data-shortid="1007">
        <em>ProductId: </em>
        <span>110-01-070-10</span>
        <br>
        <em>ShortID: </em>
        <span class="ShortID" data-shortid="1007">1007</span>
        <br>
        <em>Product Defaul cat: </em>
        <span class="spanDefCat" data-defcat="334">334</span>
        </div>
        </span>
        </li>
        <li id="RadListBox1_i3" class="rlbItem ui-draggable">
        <li id="RadListBox1_i4" class="rlbItem ui-draggable">

    </ul>

I need to find this element i was using this function before

$("#AllProducts li:contains('ShortID:" + Product + "'):contains('" + iCategory + "')").addClass("backDefaultCat");

But now as you see i working with data-shortid=X sow i need to check if item have data-shortid=X and data-defcat=Y

some think like this

  $("span.ShortID[data-shortid=" + Product + "] span.spanDefCat[data-defcat=" + iCategory + "]").parents("li:first").addClass("backDefaultCat"); 

I just can combine it right.

Upvotes: 0

Views: 134

Answers (2)

Rajnikant Kakadiya
Rajnikant Kakadiya

Reputation: 2265

Try to replace with below line. Just added , which you forgot :)

$("span.ShortID[data-shortid=" + Product + "], span.spanDefCat[data-shortid=" + iCategory + "]").parents("li:first").addClass("backDefaultCat");

http://jsfiddle.net/SBhMV/7/

updated your jsFiddle please check.

http://jsfiddle.net/SBhMV/9/

Updated once again

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

Try

$("#AllProducts li").filter(function(){
    return $(this).has('[data-shortid="' + Product + '"]').length > 0 && $(this).has('[data-defcat="' + iCategory + '"]').length > 0
}).addClass("backDefaultCat");

Demo: Fiddle

Or

$('#AllProducts li:has(span.ShortID[data-shortid="' + Product + '"]):has(span.spanDefCat[data-defcat="' + iCategory + '"])').addClass("backDefaultCat");

Demo: Fiddle

Upvotes: 2

Related Questions