-2
<div class="A1">
    <div id="child1">A1-1</div>
    <div id="child2">A1-2</div>
    <div id="child3">A1-3</div>
    <div id="child4">A1-4</div>
    <div class="A2">
        <div id="child5">A2-1</div>
        <div id="child6">A2-2</div>

        <div class="A3">
            <div id="child7">A3-1</div>
            <div id="child8">A3-2</div>
        </div>
    </div>
</div>

so this is the code I have, I am looking for a way to get the number that comes after the word "child" in each ID. I was thinking about using something like var values = $(‘id:contains(child)’).attr(); but it doesn't work. or maybe using regex? any suggestions? thank you

cppit
  • 4,478
  • 11
  • 43
  • 68
  • 1
    You need to read the jQuery documentation on selectors and the `.attr()` function. – BoltClock Mar 01 '12 at 08:34
  • 1
    This kind of question asked and answered already... Take a look at; http://stackoverflow.com/questions/190253/jquery-selector-regular-expressions – Lost_In_Library Mar 01 '12 at 08:34
  • 1
    similar to this one? http://stackoverflow.com/questions/3086554/find-all-elements-based-on-ids-using-regex-on-jquery-selector – robasta Mar 01 '12 at 08:36

2 Answers2

2

JQuery has a concept of selectors.

You can use the following code:

("[id*='child']").each(function() { alert(this.attr()); });

This is called a "Contains Selector". JQuery also supports EndsWith, Child, Parent etc selectors. For more info please visit http://api.jquery.com/category/selectors/

Community
  • 1
  • 1
Ameya
  • 627
  • 4
  • 6
1

You can also try :

$('div[id^="child"]').each(function(){alert(this.attr());})

The ^ means "start with". So it will select any id starting with 'child'

http://api.jquery.com/attribute-starts-with-selector/

Jeroen
  • 579
  • 5
  • 19
Nasruddin
  • 1,640
  • 14
  • 20