Here is a solution: You can use JQuery selector $("[id='idofelement']") to select all the elements with matching ID.
getElementById() The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.
In jQuery, you can use the . length property to check if an element exists. if the element exists, the length property will return the total number of the matched elements. To check if an element which has an id of “div1” exists.
jQuery prev() Method The prev() method returns the previous sibling element of the selected element. Sibling elements are elements that share the same parent. The DOM tree: This method traverse backwards along the previous sibling of DOM elements.
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
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!
Select all div
elements whose id
attribute contains the string triger
:
$('div[id*="triger"]');
There's more about using *=
in the jQuery documentation: Attribute Contains Selector [name*="value"]
var trigerList = $("div[id^='triger']");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With