tags:

views:

322

answers:

3

I have the following jQuery which I want to output a list of images. The problem is that I cannot get 'this' to find the source. It currently is an object which outputs as an HTMLImageElement. How can I get the image source from this object?

$("#imgs li.images img").each(function(i) { 
    $("#list").append("<li><img src=\""+this.attr("src")+"\" /></li>");
});

I currently get the error that this.attr is not a function.

+3  A: 

this is the DOMNode, not a jQuery object. You can access this.src immediately, or, if you want to use jQuery, $(this).attr('src'), although would be a detour for doing the same thing.

David Hedlund
arrgh figured it was something small that I was overlooking.
corymathews
+1  A: 

Use $(this) instead of this.

Petr Peller
A: 

The problem is your use of 'this'. It is a special keyword used in object orientated programming to refer to an objects own properties

thecoshman