Find whether an element contains in a jquery selector

Let say I have,

<a id="a123"></a>

Now I need to check whether $("a") selector contains $("#a123") element.

Upvotes: 2

Views: 111

Answers (3)

ShankarSangoli
ShankarSangoli

Reputation: 69915

If you want to find whether a has this a123 id then you can do it using is method which returns true if it matches the selector else returns false.

if($("a").is('#a123')){
     //It is #a123
}

If you want to find if a contains child with id a123 then use has method which reduces the set of matched elements to those that have a descendant that matches the selector or DOM element..

if($("a").has('#a123').length > 0){
     //It contains child with #a123
}

References:

Upvotes: 3

Nick G.
Nick G.

Reputation: 1175

You can use.attr("id") to pull back the value of the id. Then you can use an if statement to check if the value is what you expected.

Upvotes: 2

Mike Edwards
Mike Edwards

Reputation: 3771

$("a").filter("#a123").size() > 0

Upvotes: 2

Related Questions