Toolsnip

What is the difference between 'this' and '$(this)' in jQuery?

Javascript Interview Questions and Answers

Short Answer

'this' refers to the current DOM element in JavaScript, while '$(this)' is a jQuery object that wraps the current DOM element.

Detailed Answer

When used outside of a jQuery event handler, 'this' refers to the global object (window in a browser), while '$(this)' will throw an error.

When used inside a jQuery event handler, 'this' refers to the current DOM element that triggered the event, while '$(this)' is a jQuery object representing that element.

Using '$(this)' allows you to access jQuery methods and properties on the current element, such as 'addClass()', 'removeClass()', and 'attr()'.

Overall, 'this' is a JavaScript keyword that refers to the current DOM element, while '$(this)' is a jQuery object that provides additional functionality for working with that element.