tags:

views:

48

answers:

4

see this code

$(function() {
    $('.do').deleteTable({
        parent: $(".do").parentsUntil("#did")

    });
});

its works but if i make it

$(function() {
$('.do').deleteTable({
parent: $(this).parentsUntil("#did")
});
});

not works; why? i replace $(".do") to $(this)

+1  A: 

"this" is probably the "window" object, which won't have parents.

I'm guessing you're thinking of a situation as follows:

$(function() {
    $('.do').click(function() {
    alert($(this).is('.do'));
    });
});

In the above case, when the function is run because of a click event, then "this" will be the $('.do') object you expect it to be. In your case however, the context of "this" is not $('.do')

David Archer
+3  A: 

{ parent: $(this).parentsUntil("#did") } is an object literal, calculated before it is passed to deleteTable.

It is not a function called back from deleteTable with this set to each element with class do.

So this is the outer function() { ... }'s this: namely window.

bobince
+2  A: 

This may help...

'this' demystified

Brett
+1  A: 

When I'm in a similar situation, the first thing I do is set up a breakpoint and check (using firebug for example) if the $(this) is really what I'm expecting.

99% of the time that helps me understand what's going on.

David V.