views:

65

answers:

4

i have some dynamic code that will make a list of divs. i want to get back a list of items to loop through that have an id with the same start of the string.

For example, i might have

<div id="table1" . . .
<div id="table2" . . .
<div id="table3" . . .

how, using jquery, can i get a list of all divs that have an id that starts with "table" ?

+5  A: 

You can use an attribute selector for this.

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

The above means find all divs with an id that starts with "table".

Generally you want to avoid attribute selectors where possible however so if this is going to be something you do a lot I'd suggest changing your HTML:

<div id="table1" class="table" . . .
<div id="table2" class="table" . . .
<div id="table3" class="table" . . .

So you can simply do:

$("div.table")...

which will be much faster.

Update: You seem to have asked (in comments) regarding looping. For this use each():

$("div[id^='table']").each(function() {
  // `this` now refers to the current element
});
cletus
That should be an equals sign Cleatus - no downvote from me though.
alex
@alex: Typo corrected. Thanks.
cletus
Typo. `$("div[id^'table']"` should be `$("div[id^='table']"` http://docs.jquery.com/Selectors/attributeStartsWith#attributevalue
artlung
@artlung: corrected that typo already. But thanks.
cletus
Always a bit of latency I suppose. :-)
artlung
+1 for answering the question *and* for showing a better way to write the code
Doug Neiner
+1  A: 
$('div[id^=table]')

I think that should do it, from memory. It uses the ^, like in a regex.

So you don't ever accidentally select more then you bargain for, I'd add some more specificity to it. For example, you might have

$('#my-table div[id^=table]')

To then loop through them, use each()

    $('div[id^=table]').each(function() {
        $(this).append('<p>hello </p>');
});
alex
+1  A: 
$('div[id^=table]')
artlung
artlung - how would a then loop through them ?
ooo
Using the for-in: http://www.w3schools.com/jS/js_loop_for_in.asp
Pindatjuh
Same way you always would loop in jQuery, `$('div[id^=table]').each(function(){ $(this).addClass('whatever'); // .. or whatever you want to do.});
artlung
@Pindatjuh, there's a built in iterator to use when you use jQuery: http://api.jquery.com/each/ , additionally for..in loops have side effects that can be negative. Not saying they don't have a use, but need to be used with http://yuiblog.com/blog/2006/09/26/for-in-intrigue/ in mind.
artlung
For in loop can be dangerous Pindatjuh - unless using `hasOwnProperty()` it can dredge up things up the prototype chain.
alex
Pindatjuh
It's all in Crockford's 'The Good Parts'. :)
alex
what makes no sense to me is that when debugging in firebug var dialogs = $('div[id^=dialog]'); this works fine but when i do this below, i skips over the each as if it cant find any entries $('div[id^=dialog]').each(function() {// var html = $(this).html(); $(this).prependTo("#beforesubmit"); })
ooo
The docs are your friend here. You don't need to use each then, I believe: http://docs.jquery.com/Manipulation/prependTo Just do: `$('div[id^=dialog]').prependTo('#beforesubmit');` "Prepend all of the matched elements to another, specified, set of elements. As of jQuery 1.3.2, returns all of the inserted elements."
artlung
+1  A: 
$("[id]").filter(function() {
    return this.id.match(/^table[0-9]+$/);
});

Perhaps?

Pindatjuh
+1 This one is pretty good - it will have a better chance of not picking up false positives. Why was it downvoted?
alex
Don't know: there was a massive downvote to all answers!
Pindatjuh
One thing that could make this answer better is using the start and finish anchors (is that what they are called??). e.g. `/^table[0-9]+$/`
alex
completely true! yep they are called anchors.
Pindatjuh