tags:

views:

21

answers:

1

Hello all,

I have a function that looks somewhat like this:

function(domObj) {
    var currentObj = $(domObj);
    ...
    currentObj.contents().find(".ws").after("foobar");
}

My problem is that the above method of using .contents().find() is not working. "foobar" never gets stuffed after the specified dom element, represented by the selector, .ws

However if I do this:

$(".ws", currentObj).after("foobar"); 

Then the string, "foobar" gets appended every time.

My question:

Are not these two methods supposed to be equivilant? How/what am I doing wrong in my use of .contents().find() so that it is not working?

Thanks!

+2  A: 
$(".ws", currentObj).after("foobar");

... is equivelant to:

currentObj.find(".ws").after("foobar");

contents() returns all child-nodes, and so therefore when you execute contents().find() you're actually searching within the child-nodes, as opposed to searching the child-nodes themselves.

J-P
hold up, then why did `.children().after()` work for me? *confused*
Alex
`children()` returns the children too, but you're not executing `children().find()`, you're just executing `children()`, so if you call `children('.ws')` you're basically saying: "gimme the children with a class of `ws`", as opposed to `contents().find('.ws')` which is saying "gimme the children, then look within those children for an element with a class of `ws`."
J-P
ah totally makes sense. Thanks.
Alex