I am trying to get the id of the clicked/shown element in fancybox. I have tried both "this.id" and "this.attr("id")" - but none of them works.
$("a.lightbox_image").fancybox({
'transitionIn': 'elastic',
'transitionOut': 'elastic',
'speedIn': 600,
'speedOut': 200,
'content': 'Id of element clicked'+this.attr("id")
});
Any suggestions?
You can do it like this:
this
refers probably towindow
where you're binding currently (ordocument
if in theready
event, can't be sure without seeing more code). Forthis
to be the<a>
like you want, you should use a.each()
loop and assign it there...inside the.each()
closure,this
refers to the anchor.This seems to work for me :)
The javascript:
In firebug, you can see the logging :)
The answers which suggest using an each loop are correct, but if you're using any of the callbacks (beforeShow, beforeLoad, onUpdate etc) there's one important detail to understand.
In the context of your each loop, this refers to the element which will get clicked. But as soon as you're inside the fancybox callback, this refers to the fancybox object. The trick is to store the elements this into another variable, e.g that. Then refer to that in your callback.
The
each(function()
solution breaks the gallery (in other words, no more Next and Prev button).A solution that seems to work and saves the gallery is to assign a function to the click event on the element before fancyBox is called and save the id to a unique div.
For instance:
I had problems using Nick Craver's solution together with "onClosed" function and after some tests I found working solution:
This, in my case, was used to reload row with data after closing fancybox window where that data was edited.