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
2010-06-11 08:06:07
You should probably use an array, not a string.
icktoofay
2010-06-11 08:07:05
Hi there.Yeah, made a mistake, just fixed it. Doh!!! Too early for me.
Jason Evans
2010-06-11 08:07:59
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");
Markive
2010-06-11 08:06:08
+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
2010-06-11 08:09:55
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
2010-06-11 08:19:17
+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')
Felix Kling
2010-06-11 08:10:18