Search code examples
javascriptjqueryimagedynamicalt

dynamically write image's alt value as text with jQuery


Trying to write a small function that updates a paragraph with an image's alt text. the .active class is the image being viewed. I'll launch this function on doc.ready and under a few .click events. Here's what i've got with no results:

$("#title").text($('.z.active').attr('alt', ''));

Thanks for your help!


Solution

  • This illustrates the basics of the jQuery in question. I assume you can handle the click events.

    HTML:

    <div class="z">
    <img class="active" alt="foobar" src="http://placehold.it/350x150"/>
    </div>
    <div id="title"></div>
    

    Javascript:

    $("#title").text($('.z .active').attr('alt'));
    

    Check it out on JSFiddle.