Kiffin
Kiffin

Reputation: 1037

Selector for a range of ids

I need to select all span tag elements within a div with an id list_{[0-9]}+ having the following form:

<div id="list_1234" ...>
    <!-- can be nested multiple levels deep -->
        ...
            <span class="list_span">Hello</span>
</div>

How can I do that, e.g. without using jQuery? Is that possible?

Upvotes: 0

Views: 2790

Answers (4)

Litek
Litek

Reputation: 4888

If you're happy with css3 selectors you could do something like

div[id^="list_"]

But this will also target divs with ids like list_foo.

Upvotes: 6

tloflin
tloflin

Reputation: 4050

The only thing you can do is:

list_1 span, list_2 span, list_3 span... { ... }

Is it possible to add a "class" attribute to these divs? That's the proper way to handle multiple elements with ids.

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630429

You can do this with pure CSS pretty easily, just give those divs a class like this:

<div id="list_1234" class="container" ...>

And CSS like this:

.container span { /* styles */ }

Upvotes: 3

remi bourgarel
remi bourgarel

Reputation: 9389

Why you do'nt use a common class ? You can add many class

class="list_1234 mydiv"

And your selector :

.mydiv span

Upvotes: 2

Related Questions