views:

1856

answers:

5

Hello everybody.

I have a structure of html 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 do I get array of all the div's in JQuery with the triger ID in them (as you can see, they all have triger but different numbering eg. triger1, triger2 etc...)

Thanks in advance

+4  A: 

you can actually use a regular expression in a selector to achieve exactly what you are looking for, as such:

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

(Note: this does require the regex-selector plugin)

Someone asked a similar question here.

You can read all about regular expressions here.

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

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

by using a simple jQuery selector. For more complex selections, though, you will need to use the regex plugin or do it manually, as noted in the linked question.

good luck!

yuval
That requires a custom filter.
Andrew Moore
yeup! already updated the answer with the link to it and an alternative way for simpler queries
yuval
A: 
var trigerList = $("div[id^='triger']");
Tim S. Van Haren
A: 
$('div[id=*"triger"]');
Chacha102
+8  A: 

You can use the following:

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

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

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

API/Selectors

Andrew Moore
Thanks. Thats exactly what i was looking for.
Dmitris
Can we use it for multiple ids with the (^=). For ex.: to get the divs that starts with either 'trigger' or 'smart'?
Prasad
**@Prasad:** Sure! Simply use `$("div[id^='trigger'],div[id^='smart']")`
Andrew Moore
A: 

Thanks thats great coz i am doing same thing on AJAX but to leanthy prec..... thanks again.

Huzoor bux