Use $('#categories > li').find('a[data-category="TestValue"]')
How to find inside a list element by attribute JQuery
-
12-07-2023 - |
Вопрос
I have this structure
HTML
<div>
<ul id="categories">
<asp:Repeater ID="repeater" runat="server" OnItemDataBound="repeater_ItemDataBound">
<ItemTemplate>
<li><asp:LinkButton ID="linkbutton" runat="server" href="javascript:void(0);"/></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
Code behind
protected void repeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton linkbutton = (LinkButton)e.Item.FindControl("linkbutton");
lnkCategoryName.Attributes.Add("data-category", ((ProductCategory)e.Item.DataItem).Description);
}
}
Inspect element from Developers tool
<ul id="categories">
<li><a id="cphContents_repeater_linkbutton_0" href="javascript:void(0);" data-category="TestValue">Value 1</a></li>
<li><a id= ....
What I want is to get the accurate anchor with data-category attribute in Jquery but I cannot do it til now.
I tried this in the console of Developers tool
$('#categories > li').children()
And it gives me all the anchors elements from all the LI but then I want to choose only the one I'm interested in.
When I do the following line, the console returns the square brackets. Help!!
$('#categories > li').children().find('a[data-category=TestValue]')
Решение
Другие советы
Check http://jsfiddle.net/yvanavermaet/98K2G/1/
HTML:
<ul id="categories">
<li><a id="cphContents_repeater_linkbutton_0" href="javascript:void(0);" data-category="TestValue">Value 1</a></li>
<li><a id="cphContents_repeater_linkbutton_1" href="javascript:void(0);">Value 2</a></li>
<li><a id="cphContents_repeater_linkbutton_2" href="javascript:void(0);">Value 3</a></li>
</ul>
JS:
var a = $("#categories > li > a[data-category='TestValue']");
alert(a.length); // this will alert "1"
Не связан с StackOverflow