Search code examples
jqueryimagesrc

Jquery : get previous image with prev()


here's my problem :

$("#gallery > img").live('click',function() {
    $(this).prev().css("background" , "#f99"); // WORKS !
    var src = $(this).prev().src;               // DOESN'T WORK (src is undefined)
});

I see in the google chrome debugger that prev() function returns a jQuery.fn.jQuery.init[1] object... It seems like it contains the HTMLImageElement i want with prev() at index 0, but working with it like an array doesn't work.

I'm lost here I could use some help... thank you guys


Solution

  • // get the DOM element and access the src property
    var src = $(this).prev()[0].src; 
    

    or:

    // get the DOM element and access the src property
    var src = $(this).prev().get(0).src; 
    

    or:

    // access the src via .attr()
    var src = $(this).prev().attr("src");