Can not write url in date attribute.

html

<div class="wrap-img"> <div class="share-link-list social-likes"> <li class="share-img fb" data-service="facebook" data-media=""></li> <li class="share-img vk" data-service="vkontakte" data-media=""></li> <li class="share-img ok" data-service="odnoklassniki" data-media=""></li> <li class="share-img pin" data-service="pinterest" data-media=""></li> <li class="share-img tw" data-service="twitter" data-media=""></li> </div> <img alt="" src="/images/examle/example.jpg" > </div> 

jquery

 $(function(){ $("body").bind("click", ".share-img", function(){ var urlImg = $( this ).closest(".wrap-img").find('img').attr('src'); $(".share-img").attr("data-media", 'http://mysite.example.ru' + urlImg); }); }); 
  • Exactly, my cant - Radick
  • corrected, but it still turns out - Radik
  • to work with data- attributes, there is a data() method, in this case, use it as data('media', value) - teran
  • the author, what do you want to do? - teran
  • one
    In fact, the data method in jquery does not add / change the attribute value. He reads it at the first access, and keeps it - Grundy

1 answer 1

 $('.wrap-img').each(function(){ var urlImg = $(this).find('img').attr('src'); var siteUrl = 'http://example.ru'; var fullUrl = siteUrl + urlImg; $( this ).find('.share-img').attr('data-media', fullUrl); });