Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with .attr('title')

I am trying to retrieve the title attribute and replace the current paragraph text with it. Everything else about my script is working perfectly. The title attribute is coming back as "undefined". I am new to javascript and I have looked all over for answers... nothing that I am seeing so far.

JS:

$(document).ready(function(){

    $('.thumbnails a').click(function(e){
        e.preventDefault();

        $('.thumbnails a').removeClass('selected');
        $('.thumbnails a').children().css('opacity','1');

        $(this).addClass('selected');
        $(this).children().css('opacity','.4');

        var photo_caption = $(this).attr('title');
        var photo_fullsize = $(this).attr('href');
        var photo_preview = photo_fullsize.replace('_large','_preview');

        $('.preview').html('<a href="'+photo_fullsize+'" title="'+photo_caption+'" style=background-image:url('+photo_preview+')></a>');

        $('.caption').html('<p><a href="'+photo_fullsize+'" title="'+photo_caption+'">View larger</a></p><p>'+photo_caption+'</p>');
    });
});

HTML:

<li>
   <a href="images/webdesign/indiePantry_large.jpg" target="_blank">
      <img src="images/webdesign/indiePantry_thumbnail.jpg" 
             width="160" height="160" title="Indie Pantry" />
   </a>
</li>
like image 453
Aaron Happe Avatar asked Nov 28 '22 07:11

Aaron Happe


1 Answers

In your code, this references the a tag, not the image.

Try:

$('img', this).attr('title');
like image 110
BLSully Avatar answered Dec 09 '22 15:12

BLSully