Jquery select all elements in regex

Hello forums

In JQuery how do you select all elements using regex.
I have this markup

<span id="td_12345">something</span>
<span id="td_12343">something</span>
<span id="td_123345">something</span>
<span id="td_1255">something</span>

How do I select all id’s starting with td_

Thanks.

I think you need the attribute-contains-selector: Attribute Contains Selector [name*=“value”] – jQuery API


$('span[id*="td_"]')

Wow thats easier than a regex solution. Thanks

If you want to ensure that it doesn’t accidentally match with something in the middle of the name, you can use the attribute-starts-with selector instead:

$(‘span[id^=“td_”]’)

It also makes things easier for you or others who come back to the code, because you can tell immediately what the code is expected to achieve.

Ooh, even better! Hadn’t seen that one. jQuery is amazing (hope DS60 doesn’t read this :wink: )