Friday, November 15, 2013

jQuery - this vs $(this)

The difference between this and $(this) is well explained in this StackOverflow answer ( link ).


In Summary,

$(this)[0] == this;

Of course, $(this) gets all the nice jQuery functions and properties, whereas this is javascript keyword.

Also, the author of the answer makes an intuitive point for the jQuery selector that "jQuery selector always returns array.

$('#MyUniqueDiv')[0]  == document.getElementById('MyUniqueDiv');



Even if there is only a single div with id="MyUniqueDiv" in the DOM, the following is still true.

$('#MyUniqueDiv')[0] == $('#MyUniqueDiv');




No comments: