Noel Noel - 1 year ago 92
Javascript Question

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

What is the difference between these 2 statements

in jquery.

Answer Source

$(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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download