views:

50

answers:

2

Hi guys,

I'm trying to get the source attribute of all images withing a specific div but somehow it keeps telling me that the function .attr() doesn't exist...

That's the function. Firebug also tells me that "this" is an image element. I'm using jQuery v1.3.2

$('#products LI DIV IMG').each(function() { 
  var image = this;
  alert(image.attr('src'));
});

Any idea how to fix that?

Thanks in advance!

+2  A: 

You have to make it a jquerby object to access attr('src').

var image = $(this);
alert(image.attr('src'));

or you can use

var image = this;
alert(image.src);
rahul
superb... thanks for your insanely fast response! :)
n00b
The explanation of this answer is that the 'this' passed to your function, from the 'each' function, is a reference to a DOM element. In order to be able to invoke the 'attr' function, you need a jQuery object, and this is obtained by using the '$(this)' construct. Hope this helps you to understand things better.
belugabob
yes it helped! thank you as well
n00b
A: 

this is indeed an image element, and you need for it to be a jQuery element:

var image = $(this);
David Hedlund