Saturday, 15 May 2010

How to get jquery object by its custom attribute -



How to get jquery object by its custom attribute -

html segment:

<ul id="nav_des"> <li class="cur">test1<a class="shopping2" data-category-id="1">12</a></li> <li>test2<a class="shopping2" data-category-id="2"></a></li> <li >test3<a class="shopping2" data-category-id="3"></a></li> <li style="margin-bottom:25px; background:none"></li> </ul>

i want text() of "" "data-category-id" 1 (i.e. 12 ) . tried utilize attribute selector failed. utilize "for" loop value, don't think it's way. how prepare it? thanks

var category_id =1 $(".shopping2").("data-category-id='"+category_id+"'").show()

you can utilize attribute-equals-selector elements attribute values:

$(".shopping2[data-category-id='"+category_id+"']").show();

working demo

jquery

No comments:

Post a Comment