$('[data-action="freepaper-flipbook"]').on('click',function(){ var button = $(this); // Try to load the related receiver and title. If we are from the view, it will find a container var container = button.parents(".pub__container"); // freepaper container if (container.length!=0) { var receiver = container.children(".receiver"); var title = container.children("h4").text(); } else { // Otherwise, we are in the publication page and there's only one receiver. var receiver = $('#receiver'); var title = $('.pub-title').text(); } var path = drupalSettings.host + "/" + drupalSettings.modulepath; var pdf=button.data('pdf'); var template = { html: path + '/templates/default-book-view.html', pdf: pdf, controlsProps: { downloadURL: pdf, }, links: [{ rel: 'stylesheet', href: path + '/css/font-awesome.min.css' }], styles: [ path + '/css/short-black-book-view.css' ], script: path + '/js/default-book-view.js', }; receiver.load(path + '/templates/flipbook-modal.html', function () { $('#flipbookModalLabel').text(title); $('#flipbook-modal').on('hidden.bs.modal', function() { book.dispose(); receiver.text(""); }); $('#flipbook-modal').modal('show'); var book = $('#flipbook').FlipBook({ pdf: pdf, template: template }); }); });