0
$(this).append('hello');

The above could be a line nested in some function of jquery. I know that this is a correct line, but I was wondering whether you could mix javascript with jquery and write it down like this.

this.append('hello');

The only difference is that in the second line of code the this keyword is not written inside the jquery method, but this should still work because the this keyword itself automatically stores the location of the node at which the function is aimed. Right? Well apparentely it does not work, so when and why do can't you use the this keyword alone?

user7548524
  • 53
  • 1
  • 1
  • 6

1 Answers1

0

The only difference as far as I understand is the wrapper, which is the jQuery object: https://learn.jquery.com/using-jquery-core/jquery-object/. This wrapper exposes jQuery methods on the element, whereas just using this returns the element, but does not expose those same methods. As a simple example, this will return the textContent of the div,

$('div').click(function() {
  console.log($(this).text());
})

Where as this will not

$('div').click(function() {
  console.log(this.text());
})
thesublimeobject
  • 1,393
  • 1
  • 17
  • 22