Search code examples
jquerycolorbox

Use title of currently displayed image as mail subject


I use the Colorbox by Jack Moore to display several images. Now I want to integrate a mailto link after the title in the lightbox. This should give visitors the opportunity to request further information about the image currently displayed. So that the mail can later be assigned to an image, the title of the image currently displayed should be the subject of the mail. At the moment I can append a mailto link to the title with jQuery and "append". However, I have not yet found a way to get the title of the image and to use it as the subject of the mail. My solution so far:

<script>
$(document).bind('cbox_complete', function(){
    $('#cboxTitle').append( "<a href='mailto:[email protected]?subject=???'>Get info</a>" );
});
</script>

Does anyone have any ideas?


Solution

  • I do not know colorbox, but I would expect

    $(document).on('cbox_complete', function(e){
      $('#cboxTitle')
        .append(`<a href="mailto:[email protected]?subject=${e.target.title}">Get info</a>`);
    });
    

    or change ${e.target.title} for

    $(e.target).closest(".somecontainer").find("img").attr("title")
    

    where you need to provide the selector for ".somecontainer"