How to loop through LI items in UL, get specific attributes, and pass them via $.ajax data
javascript, jquery
Solution
There's an `.map` for that:
var ids = $("#gallery li").map(function () {
return this.id.split("-")[1];
}).get().join(",");
...
$.ajax({
url: '/ajax/upload.php',
type: 'POST',
data: { ids: ids },
success: function(data) {
}
});
Demo.
Problem
Here is my HTML code: ``` <ul id="gallery"> <li id="attachment-32" class="featured"><a href="..." title=""><img src="..." alt="" /></a></li> <li id="attachment-34"><a href="..." title=""><img src="..." alt="" /></a></li> <li id="attachment-38"><a href="..." title=""><img src="..." alt="" /></a></li> <li id="attachment-64"><a href="..." title=""><img src="..." alt="" /></a></li> <li id="attachment-75"><a href="..." title=""><img src="..." alt="" /></a></li> <li></li> </ul> ``` Here is my sample ajax request: ``` $.ajax({ url: '/ajax/upload.php', type: 'POST', data: { ... }, success: function(data){ } }); ``` Here is what I want to achieve. How to get the attachment number in ID attribute for every LI inside the gallery UL only when an ID attr is there and pass them via ajax data in this way: `{ attached : '32,34,38,64,75' }` if there is a better way of doing this let me know I want to pass the list items which contain attachments to process. I want to avoid LI items which are empty, do not include id attr. How to get the list item LI which has class of featured e.g. { featured_img : .. } and pass the attachment ID number if featured LI exists, and if none of list items is featured pass featured_img with 0. So i know how to process it via php in the request. Any help is appreciated. Thank you.