views:

107

answers:

3

Hello,

I have some DIVs with ID "div1", "div2" ... "divx". I want to perform an operation on them. Is there any way to get the element ID .contains "div". Any other idea?

+10  A: 

You can use the starts with selector

$("div[id^='div']")

The above will grab all divs that have an id attribute that begins with the letters div.

jsFiddle example that makes all divs with id div... invisible.


Here's how to handle the trickier case if you want to match div followed by a number but not div followed by something else. So div1 would match, but divx would not.

In this case we use filter() with a function. The function should return true for when we want a match and false for when we do not. match() and a regex is great for this (you can use [0-9] to represent a digit in a regex or the simpler [\d]):

$("div").filter(function() {
    return $(this).attr("id").match(/^div[\d]+$/)
})

attr() returns the value of the specified attribute, in this case id.

jsFiddle example that makes all div followed by number disappear, but not div followed by other things.

Peter Ajtai
While Peter's excellent answer's are totally correct, the *best* thing to do is add a class of "mydivs" or whatever (in the HTML) to all these divs so you can select them by class. That's why classes exist, to group like items. :)
Paul Irish
+7  A: 

You could use this (I haven't tested it though):

jQuery("div[id^='div']")

This will get all div elements that have an id beginning with "div".

If you wanted all the div elements that have an id containing "div", then you could use this:

jQuery("div[id*='div']")
Dumb Guy
+2  A: 

Using ID's like div1, div2, div3 is a bad practice. It's like naming your classes "black-bold-12px" and then assigning those styles to that class. It misses the point on semantics.

The right way for doing this would be using a class for all of them.

Elements can be of more than one class, using a space as separator:

<div class="something usethis">...</div>
<div class="usethis something_else">...</div>
<div class="usethis">...</div>
<div class="something anotherclass usethis" id="someId">...</div>

<script>
  $(".usethis").html("New contents for all of them!");
</script>
Sebastián Grignoli
The other answers answer the question, but this is the RIGHT answer
box9
If you have a series of posts, having IDs like `post-1`, `post-23` etc. where the number is a unique universal post identifier seems perfectly legitimate and semantically informative... But yeah, it's certainly easier to work with well thought out classes when possible. ---- However, speaking of semantically well formed pages, external Javascript is superior to using the <script> tag ;)
Peter Ajtai
@Peter Yes, that's what I do if I need to identify every single one of them. Anyway, if you think about it, having IDs like "post-1" is not the same as havig IDs like "div-1"... Still, when refering to them as a group it's better to use a class.
Sebastián Grignoli
@Sebas - Just to be clear, I completely agree with you, and you make a great point overall.
Peter Ajtai