CIRCLE
CIRCLE

Reputation: 4879

Store in array all img tag id's

I'm trying to store in an array all the id tags from images inside a list using jquery.
So far I have this:

HTML

<div id="trash" class="ui-widget-content grid_8 ui-droppable">
    <ul class="gallery ui-helper-reset">
        <li class="" style="display: block; width: 48px;">
            <img id="22" src="data:image/jpeg;base64," width="96" height="72" style="display: inline-block; height: 36px;">
        </li>
        <li class="" style="display: block; width: 48px;">
            <img id="24" src="data:image/jpeg;base64," width="96" height="72" style="display: inline-block; height: 36px;">
        </li>
        <li class="" style="display: block; width: 48px;">
            <img id="23" src="data:image/jpeg;base64," width="96" height="72" style="display: inline-block; height: 36px;">
        </li>
    </ul>
</div>
<div id="stored" style=""></div>

JQUERY

$(document).ready(function() {
  $('#trash').click(function(){
    var imglen = $('#trash li img');
    var aux = [];
    for (var i = 0; i < imglen.length; i++) {
      aux.push(imglen[i].attr("id"));
    }
    $('#stored').text(aux.join(" "));
  });
});

It isn't displaying anything. I only got to display the first img id using the bellow code:

$('#trash').click(function(){
  $('#stored').html($('#trash li img').attr("id"));
});

Upvotes: 0

Views: 1253

Answers (3)

Shyam Shinde
Shyam Shinde

Reputation: 299

try this we'll get the ids of all image tag into the array

$(function () {

    $("#trash").click(function () {
       var aux = [];
      $('#trash ul li').each(function () {
        aux.push(($(this).find("img").attr("id")));
   });
  $("#stored").text(aux.join());

});
});

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388416

Use .map()

var imglen = $('#trash li img');
var aux = imglen.map(function(){
    return this.id
})

Upvotes: 3

DevlshOne
DevlshOne

Reputation: 8457

var imgList = $('#trash').find('img').attr('id');
$.each(imgList,function(i,v) {
    console.log(v);
});

Upvotes: 0

Related Questions