tags:

views:

53

answers:

3

In JQuery, how we can pass entire $(this) to a functions? Possible?

A: 

The $(this) element is exposed in most jQuery event operations. You can add a functional parameter to your jQuery handlers.

$("a.someClass").live("click", function(e) {
    // $(this) is a reference to the same <a> element.
    var link = $(this);
    doSomethingToLink(link);
});
Jarrett Meyer
Completely unrelated and on top of that, wrong: `e` is **not** a reference to the anchor, it is an **Event** Object.
Marko Dumic
sorry, you're right. fixed my example
Jarrett Meyer
A: 

Your question isn't very specific, but maybe you want something like this:

$("a.myLinkClass").click(
    function(){
        $(this).addClass('myClickedLinkClass'); // 'this' refers to the anchor element that was clicked
    }
);
inkedmn
+3  A: 

Um, it depends on what you are passing, but have you just tried the following:

function do_something_cool(jquery_link_object) {
  /* I do something awesome! */
}

$('a.my_link_class').click(function() {
  do_something_cool($(this));
});

This will pass the clicked link to the do_something_cool method.

Topher Fangio