1

I'm trying to get the data-shareurl from the link below for use in jQuery but for some reason when I put the variable into an alert it is always undefined. Can anyone tell me what I've got wrong? Thanks. I've made a fiddle here

Html

<a title="" class="fancybox" data-shareurl="www.google.com" href="http://fancyapps.com/fancybox/demo/1_b.jpg">
<img src="http://fancyapps.com/fancybox/demo/1_b.jpg" title="" /></a>

jQuery

 var shareurl = $(this).attr('data-shareurl'); //Get this url
 $(".fancybox").fancybox({

   beforeShow: function() {
     this.title = '<div class="addthis addthis_toolbox addthis_default_style "><a href="' + this.href + '" addthis:url="' + this.href + '" addthis:title="' + this.title + '" class="addthis_button_preferred_1"></a></div>';

     alert(shareurl); //see the variable


   },

   afterShow: function() {
     addthis.toolbox(
       $(".addthis").get()
     );
   },
   helpers: {
     title: {
       type: 'inside'
     }
   }

 });

2 Answers 2

1

Here you go with a solution

$('a').click(function(){
  console.log($(this).data('shareurl'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a title="" class="fancybox" data-shareurl="www.google.com" href="http://fancyapps.com/fancybox/demo/1_b.jpg">
<img src="http://fancyapps.com/fancybox/demo/1_b.jpg" title="" /></a>

Hope this will help you.

Sign up to request clarification or add additional context in comments.

Comments

0

You are using this as a selector. I assume that this code is outside the function $('.fancybox')...

Why not use the id or class as a selector.

var shareurl = $('.fancybox').attr('data-shareurl'); //Get this url

Comments

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.