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>
In your code, this
references the a
tag, not the image.
Try:
$('img', this).attr('title');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With