Reputation: 3401
I have following structure.
<ul id="main">
<li id="a"/>
<ul id="main1">
<li id="b"/>
</ul>
</ul>
In above code when the user clicked on the button, I want to check whether li
with id=b
present in id=main ul
or not using jquery. So pleases let me know how can I check using jquery.
Upvotes: 0
Views: 56
Reputation: 1275
Try this for a check:
if($('#main').has('li[id="b"]').length > 0)
This will check, if main has any li element with id equals b. The result is an array with all elements or zero elements.
Upvotes: 1
Reputation: 499
You can use below code to achieve what you want
if ($("ul[id='main']").find("li[id='b']").length > 0)
{
// Element is present.
}
else
{
// Element is not present.
}
Upvotes: 1