Here’s a tiny example. I want to declare t as $(this), so I can use it inside both click and mouseout events.
$('div a').on({
click: function() {
t.find('div').show();
},
mouseout: function() {
t.find('div').hide();
}
});
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the general case: no, you can’t do it.
Using a map to create multiple event handlers is just a shortcut, with the end result being separate functions assigned to each event. When one of the events occurs and the corresponding function is executed
thisis set by jQuery to be the element that the event was triggered on, so there’s no way to know ahead of time which of the elements that match the$('div a')selector is the right one.In the special case where you know that your selector will always return exactly one element you can do this:
(Of course that doesn’t work if the selector returns multiple elements because then
tcontains all the matching elements and not just the currentthisat the time of the event.)EDIT: Because it’s late at night and I’m feeling silly, here’s a way that the original map from your question could be used such that
twould be$(this). Clearly this is ridiculous given all the additional wrapping needed to make it work, but like I said, I’m feeling silly – your map is in the variablem: