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
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:
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
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"]
var trigerList = $("div[id^='triger']");