Code4life
Code4life

Reputation: 67

how do I assign an empty string to an attribute

This doesn't take into account alternative or error cases. If there is no alt attribute from which to grab, then the main image will contain the previous image's alt text. I want to clear out that alt text by assigning an empty string to that attribute, e.g. attr.alt = (not_undefined) ? thumb.attr() : '';

How do I do this?

$(function() {
$('.component-individual-detail-profile').each(function() {
    var imgContainer = $(this).find('.photos');

    imgContainer.find('li').click(function(e) {
        e.preventDefault();
        var thumb = $(this).find('img');
        var attributes = {
            src: thumb.attr('src')
        };

        if (typeof thumb.attr('alt') !== 'undefined') {
            attributes.alt = thumb.attr('alt');
        }
        if (typeof thumb.attr('title') !== 'undefined') {
            attributes.title = thumb.attr('title');
        }
        imgContainer.find('.selected').attr(attributes);
    });
});

});

Upvotes: 0

Views: 141

Answers (1)

alcfeoh
alcfeoh

Reputation: 2257

thumb.attr('alt', '')

The attr() method is a getter when used with one parameter. It becomes a setter when used with two parameters, the first being the attribute name and the second the value to set. In that case, an empty string is used.

Upvotes: 1

Related Questions