1

I am using an external JQuery library called 3D RealFlipbook (from Codecanyon). It works great, but I'm trying now to generalize my solution to get the PDF url from using the 'data-pdf' attribute which is dynamic. However, it seems like the function cannot get the attribute when I click on my button. What am I doing wrong here?

Here's the code:

<button data-pdf=http://example.com/myPdfUrl.pdf" class="flipper">VIEW MAGAZINE</button>



$(".flipper").flipBook({
        pdfUrl: $(this).attr("data-pdf"),
        lightBox:true
    });

If I do the following, the attribute shows up properly:

$(".flipper").click({
            alert($(this).attr("data-pdf"))
        });
3
  • In $(".flipper").flipBook({ pdfUrl: $(this).attr (with no other provided details) - this will be window Commented Jul 3, 2021 at 12:36
  • @freedomn-m It doesn't work :/ Commented Jul 3, 2021 at 12:40
  • What doesn't work? I've just commented on what the issue is, the answers are for answers. Commented Jul 3, 2021 at 15:15

1 Answer 1

1

When you need element specific data to pass to a plugin a simple approach is initialize inside an each() loop where each individual element instance is exposed.

$(".flipper").each(function(){
    $(this).flipBook({
        pdfUrl: $(this).attr("data-pdf"),
        lightBox:true
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant, this works perfectly. Thanks! Question is, why does this one work and the other one doesn't?
Because the context of this in your approach is window or document whereas within each jQuery sets context as individual element

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.