Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FancyBox, Obtaining an ATTR of the item clicked

I have the following lines of code:

    $("a.quickview").fancybox({
        ajax : {
            type    : "POST",
            data    : 'itemID=' + $(this).attr("name")
        }
    });

Which is binding on:

<a name="614" class="quickview" href="/URL/index.cfm">quick view</a>

I want data to post as itemID=614, but $(this).attr("name") is coming back as undefined? What am I doing wrong?

Thanks

like image 238
AnApprentice Avatar asked Mar 28 '10 17:03

AnApprentice


3 Answers

If you are using Fancybox 2.0, you can do this:

$("a.quickview").fancybox({
    ajax : {
        type    : "POST",
        data    : 'itemID=' + $(this.element).attr("name")
    }
});]

Source: Code snippet from fancyBox Github issue (this works) =>

$(".fancybox").fancybox({
  beforeShow : function() {
    this.title = $(this.element).attr('fancybox-title');
  }
});
like image 124
Jonathan Lin Avatar answered Sep 30 '22 02:09

Jonathan Lin


This did it for me:

onComplete: function( instance, slide ) {

// Clicked element
console.info( slide.opts.$orig );

}

like image 21
Auzzzy Avatar answered Oct 03 '22 02:10

Auzzzy


The way you wrote it $(this) is not what you expect. this in this context isn't the a clicked on but whatever this is when you run $("a.quickview").fancybox({...}).

Use this code instead

$("a.quickview").each(function() {
    var a = $(this);
    a.fancybox({
        ajax : {
            type    : "POST",
            data    : 'itemID=' + a.attr("name");
        }
    });
});

or if there is only one a.quickview you want to bind fancybox to use this simpler code

var a = $("a.quickview");
a.fancybox({
    ajax : {
        type    : "POST",
        data    : 'itemID=' + a.attr("name");
    }
});
like image 23
jitter Avatar answered Oct 01 '22 02:10

jitter