jeudi 4 septembre 2014

Passing $(this) to jQuery .hover() functions


Vote count:

0




I wrote a simple tooltip functionality, to be seen here .


The thing is, in both handler functions set in .hover() I need access to $(this) and other 2 variables based on it. In order do achieve that, I declare the same 3 variables in both handlers:



$('a').hover(
function () {
var $this = $(this);
var link_offset = $this.offset();
var link_tooltip = $this.data('tooltip');
// Rest of the code
},
function () {
var $this = $(this);
var link_offset = $this.offset();
var link_tooltip = $this.data('tooltip');
// Rest of the code
}
);


DRY principle should be respected, so my question is: Is there a other and smarter/less dirty way of passing the same variables set to both functions within .hover()?


Obvioulsy, the variables can't be global (and globals are evil anyway).


Any ideas how to achieve this with jQuery or pure JS?



asked 54 secs ago







Passing $(this) to jQuery .hover() functions

Aucun commentaire:

Enregistrer un commentaire