Why is it important to use $(this) instead of re-selecting the class?
I am using a lot of animate and css editing in my code, and I know I can simplify it by using $(this).
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.
When you perform an DOM query through jQuery like
$('class-name')it actively searched the DOM for that element and returns that element with all the jQuery prototype methods attached.When you’re within the jQuery chain or event you don’t have to rerun the DOM query you can use the context
$(this). Like so:$(this)will hold the element that you originally requested. It will attach all the jQuery prototype methods again, but will not have to search the DOM again.Some more information:
Web Performance with jQuery selectors
Quote from a web blog that doesn’t exist anymore but I’ll leave it in here for history sake: