I want to get the title attribute from the image and add it to the adjacent link

So far I have this:

var imageTitle = $("a.articleimages img").attr("title");

$('a.articleimages').attr('title', (imageTitle));

The problem is that it takes the first name of the image and repeats it for each link. I can do this with an ID, but it creates a lot of code because you have # image1, then # image2 and so on. Which seems excessive and stupid to me, there should be an easier / better way. Perhaps there is some way to find out which image was clicked and get the name out of it.

I want to do this so that users do not need to re-specify the title of the image so that it can be seen on Fancybox.

If there is a way to change the fancybox and get it to grab the sketch title and show that for a full-sized image then Id would be happy with that too.

thank

+3
3

, img a, title, - :

$('a.articleimages img').each(function(idx, element) {
   $(element).parent().attr('title',$(element).attr('title'));
});
+2
$("a.articleimages img").each(function() {
  var $this = $(this);
  $this.parent().attr('title', $this.attr('title'));
});
+2
var imgs = $("a.articleimages img");

imgs.each(function(i, ele){
    var img = $(ele);
    var title = img.attr('title');
    img.parent('a').attr('title', title);
});
+1
source

Source: https://habr.com/ru/post/1780568/


All Articles