Guru Guru - 7 months ago 11
Javascript Question

Jquery set attribute to every element

I want to set attribute of every a tag as img tag's src.

Here is what I did.

[https://jsfiddle.net/w69obhta/][1]


But every a tag has same href.

How can I fix this

Answer

You should go throught all the images and asign it one by one (That's using your own code). You can do something like:

$(document).ready(function() {
  $(".single img").each(function() {
       var href=$(this).attr('src');
          $(this).parent().attr('href',href);
  })

    $(".single").fancybox({
          helpers: {
              title : {
                  type : 'float'
              }
          }
      });

})