I’ll try to answer these as concisely as possible:
-
Cache it when it’s used often, especially in a loop situation, running the same code to get the same result is never a good thing for performance, cache it.
-
Use
thiswhen you only need a DOM element and$(this)when you need the jQuery methods (that wouldn’t be available otherwise), your example ofthis.idvs$(this).attr("id")is perfect, some more common examples:- Use
this.checkedinstead of$(this).is(':checked') - Use
$.data(this, 'thing')instead of$(this).data('thing') - Any other case where creating a jQuery object isn’t useful basically.
- Use
-
Decending from an ID selector is preferred for performance…how specific do you need to be? That completely depends, in short: be as specific as you need to be.