JQuery Programmer Question:

Download Job Interview Questions and Answers PDF

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

JQuery Programmer Interview Question
JQuery Programmer Interview Question

Answer:

Refer the following example:
$(document).ready(function()
{
$('#clickme').click(function()
{
alert($(this).text());
alert(this.innerText);
});
});

► this and $(this) references the same element but the difference is that "this" is used in traditional way but when "this" is used with $() then it becomes a jQuery object on which we can use the functions of jQuery.
► In the example given, when only "this" keyword is used then we can use the jQuery text() function to get the text of the element, because it is not jQuery object. Once the "this" keyword is wrapped in $() then we can use the jQuery function text() to get the text of the element.

Download JQuery Programmer Interview Questions And Answers PDF

Previous QuestionNext Question
Explain what does dollar sign ($) means in jQuery?Explain .empty() vs .remove() vs .detach()?