hedka77
hedka77

Reputation: 117

Get the id of an anchor when call fancybox from class attribute

I'm trying to get the id from an anchor when calling a fancybox from class attribute, is there any way to do this or is neccesary to get it from the click event? I already tryed with $(this) but not working and using $(".nameOfTheClass").attr("id") neither

Here's my code:

$(".cerrar_actividad").fancybox({
    'scrolling'     : 'no',
    'titleShow'     : false,
    'helpers'       : {
        title  : null
    },
'beforeLoad'    : function() {
    var str  =  $(".cerrar_actividad").attr("id").split("_");
    var id_actividad = str[1];
    alert(id_actividad);
},
...
});

and the html code:

<a href="#cerrarActividad" class="cerrar_actividad" id="actividad_<?php echo $a['id_actividad'] ?>"><img src="..." /></a>

I need this because I need to fill one form getting the id information.

Upvotes: 1

Views: 1468

Answers (2)

JFK
JFK

Reputation: 41143

You don't need to use .each(), just get the element's ID within the afterLoad (beforeLoad would work too) callback like

$(".cerrar_actividad").fancybox({
    afterLoad: function () {
        alert($(this.element).attr("id"))
    }
});

See JSFIDDLE

... or use alert($(this.element).attr("id").split("_")[1]) as in your example

Upvotes: 1

Jason P
Jason P

Reputation: 27012

You'll need to iterate the elements:

$(".cerrar_actividad").each(function () {
    var id_actividad = this.id.split("_")[1];            
    $(this).fancybox({
        'scrolling': 'no',
        'titleShow': false,
        'helpers': {
            title: null
        },
        'beforeLoad': function () {
            alert(id_actividad);
        }
    });
});

Upvotes: 0

Related Questions