Get an array of values ​​using jQuery?

Possible duplicate:
How to get all IDs using jQuery?

How can I get an array of ID attribute values ​​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> 
+4
source share
3 answers

You can use the jQuery map function. This will return an array of "English", "Spanish" and "German".

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

For a list of identifiers you can do this

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

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

+20
source

Not quite sure what you want? If you want all identifiers to be in this way:

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

+1
source

Perhaps it will be a little late:

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

Source: https://habr.com/ru/post/1403215/


All Articles