wang hai peng
wang hai peng

Reputation: 287

caption takes value from image alt attribute

I have this page in which the Jquery code is taking the alt value out and put it into a caption div under the thumbnail. but this code works only for the first one. now I want those caption to show under each pictures. ps. the number of the pictures vary, it could be 10 or may be 20.

the page url is here:

http://sydneyplants02.sajuk.com/Untitled-1.html

many thanks

Upvotes: 0

Views: 66

Answers (3)

russellc
russellc

Reputation: 494

Here's my solution

$(function(){
    $('td.photogalleryItem').each(function(e) {
        var $caption = $('<div class="caption">');
        $caption.text($('img', $(this)).attr('alt'))
        $(this).append($caption);
    }
});

This will loop through each td.photogalleryItem and add a caption with the correct alt text. Your initial code was a start, but the issue was that it was only getting the alt text of the first td.photogalleryItem.

Upvotes: 0

codingrose
codingrose

Reputation: 15699

Try:

$(document).ready(function(){
    $("td.photogalleryItem").each(function(){
        $(this).find(".caption").text($(this).find("img").attr("alt"));
    });
});

Upvotes: 0

Enjoy ..!!

$(document).ready(function () {
    $('.photogalleryItem').append(function () {
        return '<div class="caption">' + $(this).find('img').attr('alt') + '</div>';
    });
});


Reference

.append()

Upvotes: 1

Related Questions