-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

3

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

Comments

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/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.