Pankaj
Pankaj

Reputation: 3401

Check if control present in main Ul tag or not?

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

Answers (2)

Tobias S
Tobias S

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

Dinesh Prajapati
Dinesh Prajapati

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

Related Questions