user3860465
user3860465

Reputation: 55

Changing the Data-caption of image in jquery

Hi friends i am trying to change the Data-caption of a in dynamic mode.

$("#btnchange").click(function(){
    var newanchor=  $(".fancybox a");
    newanchor.attr('caption', 'i am new caption');
});

When I clicked on the image to open it in third party plugin, tool called(fancybox), it's caption is not displaying.

But when I tried to add data caption by default it like:

<a id='a1' class="fancybox" data-caption='hey' 
 href="http://farm8.staticflickr.com/7171/6417719753_374653e28c_b.jpg">
    <img src="http://farm8.staticflickr.com/7171/6417719753_374653e28c_m.jpg" alt=""/>
</a>

Then it is showing perfectly. See my fiddle:

http://jsfiddle.net/RyTcS/401/

Upvotes: 2

Views: 1455

Answers (3)

Anoop Joshi P
Anoop Joshi P

Reputation: 25537

You have the wrong selector, Your current selector will chek for the anchor inside the class fancybox .So change the code like this,

 $("#btnchange").click(function () {
      $("a.fancybox").attr("data-caption", "i am new caption");

 });

Demo

Upvotes: 0

martynas
martynas

Reputation: 12290

$(document).on('click', '#btnchange', function() {
    $('a.fancybox').data('caption', 'i am new caption');
});

You might find this example useful

Upvotes: 0

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

Try to use .data() instead of .attr(),

newanchor.data('caption', 'i am new caption');

DEMO

And as well as your selector should be .fancybox instead of .fancybox a

Upvotes: 1

Related Questions