there is a tree:

<div class="grid"> <a href=""> <img src="qwerty1" width="450" height="301" /> </a> <a href=""> <img src="qwerty2" alt="" /> </a> <a href=""> <img src="qwerty3" alt="" /> </a> </div> 

How can you drive in the values ​​of the src tags, so that the href attribute of each link corresponds to the src tag of the attached image, please help)

    2 answers 2

    You need to select all a elements that have a child img element and set the attribute for all selected elements

     $("a:has(img)").each(function() { this.href = $('img', this).attr('src'): }); 
    • @Igor Corrected. Need each () - Anton Shchyrov

    I haven't used jQuery for a long time, so I can lie in the nuances, but the general principle is something like this:

     $(".grid a").each( function(i1, o1) { $(this).find('img').each( function(i2, o2) { $(this).parent.attr('href', $(this).attr('src')); } ); } ); 

    The code is far from ideal, if there are several pictures inside, then the url is inserted last, so that this does not happen, you need to add additional checks.