matt matt - 1 year ago 99
jQuery Question

jQuery or CSS selector to select all IDs that start with some string

How can I select all elements whose

starts with "player_"?

I have multiple elements like this:

<div id="player_290x3dfda">text</div>

has a unique stamp on it. How can I select all those elements, either with jQuery or with pure CSS?

Answer Source

Normally you would select IDs using the ID selector #, but for more complex matches you can use the attribute-starts-with selector (as a jQuery selector, or as a CSS3 selector):


If you are able to modify that HTML, however, you should add a class to your player divs then target that class. You'll lose the additional specificity offered by ID selectors anyway, as attribute selectors share the same specificity as class selectors. Plus, just using a class makes things much simpler.