beardedlinuxgeek
beardedlinuxgeek

Reputation: 1662

Generic jQuery callback?

Is there a way to have a callback on a jQuery object that doesn't do anything else. something like:

$("div", this).do(function(){
    $(this).hide();
});

The only way I know how to do that is:

var obj = $("div", this);
$(obj).hide();

Upvotes: 2

Views: 266

Answers (3)

Malfist
Malfist

Reputation: 31815

Yes you can, but what you're probably looking for is the each function

$('div', this).each(function(){
  //do something with all the divs inside this
});

Upvotes: 1

user113716
user113716

Reputation: 322622

You can use the each()[docs] method

$("div", this).each(function(){
    // perform some function on each element in the set
    $(this).hide();
});

This is useful if you need to run some custom code on each element in the jQuery object.

If all you need is to call another jQuery method like .hide(), then you don't need .each(). Most jQuery methods will operate on all elements in the set automatically. They call this "implicit iteration".

Upvotes: 2

SLaks
SLaks

Reputation: 888293

It sounds like you're trying to write

$(this).find("div").hide();

Upvotes: 2

Related Questions