Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript [Solved] Get "alt" text for multiple images and apply to parent figure

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #153813
    Paulie_D
    Member

    My JQuery is pretty bad but I’ve managed how to figure out how to grab the alt text of an image and apply it as a data-attribute to the parent figure.

    What I cannot figure out is how to iterate through multiple figures / images and update accordingly.

    I think it has something to do with each but it’s beyond me at the moment.

    My basic JQ function:

    (function($) {
        var newTitle = $("figure > img").attr("alt") 
        $( "figure" ).attr("name", (newTitle) );  
    })(jQuery);
    

    Codepen

    Any assistance would be appreciated.

    #153826
    Paulie_D
    Member

    Wow…this is great.

    Thank you so very much.

    #153837
    nkrisc
    Participant

    For my own understanding, Mottie, your first example selects the parent and then gets the attribute of the child to then apply to this (the parent), while your second example begins with selecting the child and then applies the attribute to the parent?

    #153841
    Paulie_D
    Member

    That’s the way I read it.

    It’s nice to have options…. :)

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.