JQuery: get all children whose id contains part of the 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 to get (for example) all the children of a #parent whose id contains the letter i .

How can i do this?

+4
source share
3 answers

Use jquery contains selector :

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

DEMO FIDDLE

+13
source

Use the attribute containing the selector :

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

Use the attribute selector:

 a[id*="i"] 

Or .filter() :

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

Source: https://habr.com/ru/post/1502303/


All Articles