NoWar
NoWar

Reputation: 37633

How to use part of the Id or Class of DOM element with jQuery selector?

How to use part of the Id or Class of DOM element with jQuery selector?

I.e. We have

id="Some_Extra_Name"

Can we use "Extra" to get all elements with jQuery?

Upvotes: 0

Views: 68

Answers (2)

Anton
Anton

Reputation: 32581

You can use attribute contains selector *

$('[id*="Extra"]')

Upvotes: 4

Denys Séguret
Denys Séguret

Reputation: 382150

Yes, you can do

$elements = $('[id*=Extra]');

If you wanted to get the elements whose id starts with "Extra", you'd do

$elements = $('[id^=Extra]');

and for the elements whose id ends with "Extra" :

$elements = $('[id$=Extra]');

Relevant documentation

Upvotes: 4

Related Questions