Noel Noel - 6 months ago 19
Javascript Question

Is there any difference between (this) and (_this) in jquery?

What is the difference between these 2 statements

in jquery.


$(this) is the current element selector of the context but $(_this) is a normal variable selector.

For eg:

  var _this = $('div').eq(0);
  console.log($(_this));//first div
  console.log($(this));//clicked element 'p'

But usually this type of variable is used like this:

  var _this = $(this)//clicked element 'p'
   //$(this) won't refer to clicked element 'p' because it's out of context
   //$(_this) will refer to clicked element