What is the best way to find out that $(this) is currently equal in jQuery?
By writing it to the console of your favorite javascript debugging tool (e.g. FireBug or the Chrome developer toolbar, for example):
console.log($(this));
which will return an object wrapped by jQuery. If you want to know more about the native object, you can use:
console.log(this);
If you are developing javascript, you should use the javascript debugging tool. alert not such a tool.
Now that you have updated your question with some source of code, it seems like you are using $(this) in a global scope. Then it will refer to the window object. If you want it to refer to some element with a click or something, you will have to subscribe to the .click event first:
$('.someSelector').click(function() { // here $(this) will refer to the original element that was clicked. });
or if you want to export this code in a separate function:
function foo() { // here $(this) will refer to the original element that was clicked. } $('.someSelector').click(foo);
or
function foo(element) { // here $(element) will refer to the original element that was clicked. } $('.someSelector').click(function() { foo(this); });
Darin Dimitrov
source share