7

Possible Duplicate:
How to get all of the IDs with jQuery?

How can i get array of values attribute ID inside "span id='enable'" if i use JQuery?

<span id='enable'>
<p id='105'>English</p>
<p id='250'>Spanish</p>
<p id='56'>German</p>
</span>
<span id='disable'>
<p id='38'>BlaBla</p>
<p id='46'>BlaBla2</p>
<p id='87'>BlaBla3</p>
</span>
1
  • I forgot to add: Please use the search before you ask a new question. Commented Mar 24, 2012 at 11:31

3 Answers 3

25

You can use Jquery's map function. This will return array of "English","Spanish" and "German".

var myArray = $('#enable p').map(function(){
   return $(this).text();
}).get();

For getting Id's list you can do this

var myArray = $('#enable p').map(function(){
       return this.id;
    }).get();

See jsfiddle http://jsfiddle.net/B3LuE/

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

4 Comments

But i need array of attribute ID: 105, 250, 56
I've edited my answer and have written about that too.
@Chuck, you have to apply map() to #enable p or #enable > p, not to #enable itself.
Yeah, I missed that... Thanks :)
1

Not exactly sure what you want? If you want all the IDs that's a way to do it:

var ids = [];
$('#enable>p').each(function(){
    ids.push($(this).attr('id'));
});​​​​​​​​​​​​​​​​​​​​​​​
alert(ids);

Comments

0

Might be a bit late on this one:

$(function() {
 var x = $('#enable p');
 var arr = [];
 for (var i = 0; i < x.length; i++) {
    arr.push(x[i].id);
 }
});​

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.