Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass $(this) as argument?

$(document).ready(function() {
    function GetDeals() {
    alert($(this).attr("id"));
}

$('.filterResult').live("click", function(event) {
    GetDeals();
});

});

What do I need to pass as argument in function GetDeals() so that I can manipulate with $(this)?

Thanks in advance!

like image 696
ilija veselica Avatar asked Jun 04 '10 08:06

ilija veselica


2 Answers

You could just use the function as your event handle:

$('.filterResult').live("click", GetDeals);

(please note, you don't use the () to call the function, so the function itself is being passed to the live() function, not its result.

Or you can use Function.prototype.apply()

$('.filterResult').live("click", function(event) {
  GetDeals.apply(this);
});
like image 199
gnarf Avatar answered Oct 30 '22 04:10

gnarf


Above solution works and absolutely no issues with that. However I believe a better pattern here is :

$('.filterResult').live("click", function(event) {
    GetDeals($(this));
});


function GetDeals(linkObj) {
    var id = $(linkObj).attr("id");
    console.log(id);
}
like image 35
rjha94 Avatar answered Oct 30 '22 04:10

rjha94