views:

164

answers:

4

I have to get all the image tags ids inside a particular div. How can I get that using JQuery?

A: 

Hi there.

Rough guess, but try:

var imgIds = new Array();

$("div#divID img").each(function(){
    imgIds.push($(this).attr('id'));
});

You haven't given the name of the div, but I've used divId as the id of the div. Simply change that to suite your needs.

Cheers. Jas.

Jason Evans
You should probably use an array, not a string.
icktoofay
Hi there.Yeah, made a mistake, just fixed it. Doh!!! Too early for me.
Jason Evans
A: 

Use a child selector. So your saying I want all of the child 'img' elements of div #myDiv

$("#myDiv > img").css("border", "3px double red");

http://api.jquery.com/child-selector/

Markive
+1  A: 
var arraysOfIds = $('#particularDivId').find('img').map(function(){
                       return this.id;
                   }).get();

// arraysOfIds has now all the id's, access it as arraysOfIds[0], arraysOfIds[1]....  
Reigel
Ahh, cool. I've learned something new with the .map() feature. Though, looking at the documentation, why do you need the .get() call at the end. I just tried your code without it and it worked fine. Newbie wanting to learn.
Jason Evans
+1  A: 

You mean you want to have the ID attribute of each image?

var image-ids = $('#id-of-div img').map(function(){
                      return $(this).attr('id');
                }).get();

If you only want to get all images that are in a certain div, it is just:

$('#id-of-div img')

Reference: map(), get()

Felix Kling
lol.... just like mine.. :)
Reigel