0

I have a div inside a foreach loop I'm using to display content in view

<div class="someclass" data-id="[email protected]">
    <p> text1 </p>
    <p> text2 </p>
<span class="otherClass embedIcon"></span>
</div>

I'm trying to target the id attribute using the

$(".embedIcon")
    .click(function () {
        html2canvas($(this).data('data-id'),{
            onrendered: function (canvas) {
                var myImage = canvas.toDataURL("image/png", 1);
                window.open(myImage);
            }
        });
    });

When I click the icon on which embedIcon class is applied to generates an image, I get an image of all divs in the iteration.

How can I get the image of a specific div when I click the icon within the div?

3
  • 1
    post full code. Commented Dec 9, 2016 at 5:40
  • it should be like this html2canvas($(this).data('id') Commented Dec 9, 2016 at 5:41
  • @Mahi I posted full code Commented Dec 9, 2016 at 5:48

2 Answers 2

1

Your id is on your parent div, not on the embedIcon, so you can use 2 options

var id = $(this).parent().data('id')
html2canvas(id, {

or

var id = $(this).closest('.someclass').data('id')
html2canvas(id, {

Update:

html2canvas accepts the element as first parameter, not the id, so you only need to pass the parent to your html2canvas call, you don't need the data-id. You can use .parent() or .closest(".someclass")

$(".embedIcon")
    .click(function () { 
        html2canvas($(this).parent(),{
            onrendered: function (canvas) {
                var myImage = canvas.toDataURL("image/png", 1);
                window.open(myImage);
            }
        });
    });
Sign up to request clarification or add additional context in comments.

5 Comments

The second option does not achieve desired result. I get this error Uncaught TypeError: node.setAttribute is not a function(…) when I try the first option
sorry I forgot the dot before the class name, updated
same error with both options?, can you do a console.log(id) before passing it to html2canvas?
I get actual value of data-id in log but error still show up in application.
@hello html2canvas accepts the element as first parameter, not the id, check the tested updated answer before.
0

If you are using data() than it should be like:

$(this).data('id');

and if using attr() than it should be like:

$(this).attr('data-id');

1 Comment

I still get same behaviour as before. Check updated question and sample code.

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.