some elements inside
so...">

JQuery how to find div list with similar id

I have an html structure like this:

<div id="triger1">some elements inside</div> <div id="triger2">some elements inside</div> <div id="triger3">some elements inside</div> <div id="triger4">some elements inside</div> 

How to get an array of all divs in jQuery with a trigger identifier in them (as you can see, they all have a trigger, but differ in numbering, for example, triger1, triger2, etc.)

Thank you in advance

+47
javascript jquery html css
Aug 04 '09 at 3:25
source share
4 answers

You can use the following:

 $("div[id^='triger']") 

This will return all <div> with id start ( ^= ) using triger .

You can get more information about the various jQuery selectors in jQuery docs:

API / Selectors

+91
Aug 04 '09 at 3:27
source share

you can use regex in the selector to achieve exactly what you are looking for, as such:

 $("div:regex(id, yourRegularExpression)"); 

(Note: this requires the regex-selector plugin)

Someone asked a similar question here .

You can read all about regular expressions here .

As others pointed out, you can achieve the same result:

 $("div[id^='partOfID']"); 

using a simple jQuery selector. However, for more complex options, you will need to use the regex plugin or do it manually, as indicated in the related question .

Good luck

+7
Aug 04 '09 at 3:27
source share

Select all div elements, the id attribute contains the string triger :

 $('div[id*="triger"]'); 

Learn more about using *= in jQuery documentation: Attribute contains selector [name * = "value"]

+5
Aug 04 '09 at 3:27
source share
 var trigerList = $("div[id^='triger']"); 
+3
Aug 04 '09 at 3:27
source share



All Articles