views:

413

answers:

1

I am looking to use Javascript or jQuery to pull the alt tag from the specific image that had been clicked in the HTML and copy its value into a caption field.

function change_image(diff){
  var newAlt = $(this).attr('alt');
  position = (position + diff + hrefs.length) % hrefs.length;
  revealImage(hrefs[position]);
  $nav.find('.counter').html(newAlt);
}

That is what I have so far and it does not work.

+1  A: 

You are using the this keyword inside your function, it will refer to the element that triggered the event if you bind it like this:

$('img').click(change_image);

But since you have an argument in your function, I think that you are invoking it with a normal function call, in that way, the context (the this keyword) is not preserved, you can:

$('img').click(function () {
  change_image.call(this, 5); // preserve the context and pass a diff value
});
CMS
I am now using this line:var newAlt = $.attr($('img').get(0),'alt');It retrieves the alt tag of one static image and I have been unable to use the (this) feature to pull the alt tag of the specifically clicked image.Any advice?
Slevin