Can someone let me know what is the difference between this and jQuery(this)? I found that my code works if I use ‘this‘ and if I use jQuery(this) it does not work. Does jQuery(this) not query for the current object and return it?
I want to know the index of the image being clicked ( I now we have the index() method, but still want it through the below logic)
Here is the full code:(edited as per request)
for(i=0;i<5;i++)
{
jQuery("#div1").append("<img src='slider.jpg'>");
}
imgArr=jQuery("#div1>img");
jQuery("#div1>img").click(display);
function display()
{
for(i=0;i<imgArr.length;i++)
{
if(this==imgArr[i])
{
alert(i);
}
}
}
Here if I replace this with jQuery(this) it does not work.
I suppose “this” is a reference to a DOM element in your first example?
jQuery(this) is actually a jQuery wrapper around one or more DOM elements. So when you compare to a DOM element, it will never be equal.
If you want the DOM element from a jQuery wrapper, use the indexer to get the first element: