Home > Mobile >  Use title of currently displayed image as mail subject
Use title of currently displayed image as mail subject

Time:03-10

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?

CodePudding user response:

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"

CodePudding user response:

Thanks to the input from @mplungjan I found the anwser: With

var text = $('#cboxTitle').text();

I can get the content from the div #cboxTitle as a variable and then insert the variable into my subject like this

<script>
$(document).on('cbox_complete', function(e){
  var text = $('#cboxTitle').text();
  $('#cboxTitle')
    .append(`<div><a href="mailto:[email protected]?subject=${text}">Anfrage</a></div>`);
});
</script>
  • Related