Trying to get the url of the picture (scr) when you hover on the block

$(document).ready(function(){ $('#newSS div div').hover(function(){ var src = $(this).attr('src'); var a = '<a class="fancyimage" href="'+src+'">go</a>'; $(this).prepend('<div class="thumb_bar clearfix">'+a+'</div>'); }, function(){ $(this).children('.thumb_bar').remove(); }); }); 

from blocks

  <div id="newSS" class="item-container"> <a class="item" href="/s/1"> <div class="thumb_container" <div class="img"> <img id="0" class="thumb" src="913.jpg"> </div> </div> <div class="item_name">name 1</div> </a> </div> <div id="newSS" class="item-container"> <a class="item" href="/s/2"> <div class="thumb_container"> <div class="img"> <img id="1" class="thumb" src="7732.jpg"> </div> </div> <div class="item_name">name 2</div> </a> </div> 

How to correctly pull the url of the picture?

if you do this

 var src = $('#newSS div div img').attr('src'); 

then I get the url of the first block

    2 answers 2

    In your sample:

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

    this is the div unit itself on which the cursor was placed. You need to do a search in this context picture.

    This is done like this: $('img', this).attr('src')

     $('#newSS div div').hover( function(){ var src = $('img', this).attr('src'); // <<< var a = '<a class="fancyimage" href="'+src+'">go</a>'; $(this).prepend('<div class="thumb_bar clearfix">'+a+'</div>'); }, function(){ $(this).children('.thumb_bar').remove(); }); 

    Already a lot of questions were related to the search for items by event. You could also search, just look at the jQuery docks

       $(document).ready(function(){ $('.item-container').hover(function(){ var src = $('img', this).attr('src'); var a = '<a class="fancyimage" href="'+src+'">go (' + src + ')</a>'; $(this).prepend('<div class="thumb_bar clearfix">'+a+'</div>'); }, function(){ $(this).children('.thumb_bar').remove(); }); }); 

      Something like this. In your example, you are adding a link inside the link. It is not right. For this, here I brought a link that is generated in an external container.