JQuery find all ids starting with a specific string and ending with a number

I would like to do something like this:

jqueryElement.find('divIdWithIterator**').each(...); 

where 'divIdWithIterator**' matches all elements with identifiers starting with 'divIdWithIterator' and ending with a number, for example:

divIdWithIterator1, divIdWithIterator2, divIdWithIterator26

What is a good way to do this in jQuery?

Thanks!

+4
source share
2 answers

Unfortunately, the regex selector is missing.

You can use the attribute-start-with selector , then filter, and check for numeric endings with search()

For instance,

 var divs = jqueryElement.find('[id^="divIdWithIterator"]').filter(function(index){ return this.id.search(/\d$/) != -1; }); 
+5
source

Next: http://api.jquery.com/attribute-starts-with-selector/

To find elements whose identifier begins with "divIdWithIterator":

 $('input[id^="divIdWithIterator"]') 

And then avoid using other elements starting with "divIdWithIterator" and you don't want to select

+3
source

All Articles