I have a click method that is likely to be used several times. In this method, I need to access the id
of the button being clicked on more than one occasion. For example:
$('.elemental-selection').click(function () {
if(currentElement !== $(this).attr('id')) {
if($(this).attr('id') === 'element-water') {
...
}
else {
currentElement = $(this).attr('id');
}
}
});
Of course for long if
blocks I could use a switch-statement
, but what I'm curious about is if the frequent use of $(this)
would be better substituted with a variable defined to be $(this)
(and whatever method I want from it). From there and onwards, I'd refer to the variable instead of this
.
So, what's the better decision? Thank you.