0

I have many divs in ids:

<div class="nameclass" id="name<?= $id; ?>">
    something
</div>

And I want to put css to all, but by id not class:

$('div.nameclass').css(...); // Not this way

$('#name<????>').css(...); // this way

How to do it?

Nips
  • 13,162
  • 23
  • 65
  • 103
  • Do u want to apply CSS to specific element or to group of elements who have ids in a range? like name1 to name10 ? – Shades88 Dec 19 '11 at 10:23

2 Answers2

5

You could use an attribute starts with selector:

$('[id^="name"]').css(...);

And if you need even more fine grained control over the selector you could use regular expressions.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

Your selector is a string, so you can build the string by addition (+):

int id = 5;
$("#name"+ id).css(...);

But if you want to select multiple items, I would suggest doing this by class and not ID as ID's are meant for 1 item, classes are meant for a group of items. You could do:

$(".nameclass").each(function() {
    if($(this).attr("id").contains("name")) {
        $(this).css(...);
    }
});
Flater
  • 12,908
  • 4
  • 39
  • 62