ojek
ojek

Reputation: 10078

jQuery: get all children whose ID contains part of string

I have a set of links:

<div id="parent">
   <a id="UserFirst" href="#"></a>
   <a id="UserSecond" href="#"></a>
   <a id="UserThird" href="#"></a>
</div>

I need a quick way of getting (for example) all the a children of #parent, whose ID contains the letter i.

How can I do it?

Upvotes: 5

Views: 23082

Answers (3)

Blender
Blender

Reputation: 298432

Use an attribute selector:

a[id*="i"]

Or .filter():

$('a').filter(function() {
    return this.id.indexOf('a') !== -1;
});

Upvotes: 3

Kiran
Kiran

Reputation: 20313

Use jquery contains selector:

$("#parent").find("a[id*='i']").each(function(){
//do something here
});

DEMO FIDDLE

Upvotes: 14

adeneo
adeneo

Reputation: 318312

Use the attribute contains selector :

$('#parent a[id*="i"]');

Upvotes: 4

Related Questions