0

I have a bunch of divs of classname foo within a div of classname fooContainer (these foo divs have been dynamically added to fooContainer).

I have a function operateOnFoo(fooObjectToBeOperatedOn) that operates on a single foo div (that is passed into operateOnFoo as a parameter).

I would like to use the jQuery each() feature to run the operateOnFoo function on all/each of the divs of classname foo within fooContainer. I have tried using calls like:

operateOnFoo( $("#fooContainter").each( ".foo" ) );

and

$("#fooContainter").each( operateOnFoo( foo ) );

and

$("#fooContainer").find(".foo").each( operateOnFoo( ".foo" ) );

but I just can't get it to work. How do I proceed?

I'm a little new to web developing, teaching myself as I go along, so I apologize if this question is overly basic - but I couldn't seem to get it working using other similar Stack Exchange posts as I have been able to do with most of my other issues.

Thanks!

5
  • You want $("#fooContainer .foo").each( operateOnFoo ); Commented Jul 3, 2014 at 2:21
  • @Bergi - operateOnFoo wants the element as the first argument which isn't how .each() calls its callback. Commented Jul 3, 2014 at 2:27
  • @jfriend00: Oh, right, I always forget that jQuery each is broken. Commented Jul 3, 2014 at 2:31
  • Is "fooContainer" an id value or a class name? Commented Jul 3, 2014 at 2:38
  • use $("#fooContainer .foo").toArray().map( operateOnFoo ); for such arity. Commented Jul 3, 2014 at 2:42

5 Answers 5

1

The .each() callback is what it is and you can't change the number of arguments to it or the order in which they appear. So, because your operateOnFoo() function wants different arguments, you can't have .each() call it directly as it is. You can work around it though like this with a stub function that makes the arguments work like you want:

$("#fooContainer .foo").each(function(index, element) {
    operateOnFoo(element);
});

Also, the single selector "#fooContainer .foo" will select all items with class="foo" that are contained within the object with id=fooContainer.


If you can change operateOnFoo() to accept the exact two arguments that .each() uses (even if you ignore the first argument), so it was declared like this:

function operateOnFoo(index, element) {...}

then you would be able to just do:

$("#fooContainer .foo").each(operateOnFoo);
Sign up to request clarification or add additional context in comments.

Comments

0

each require a function call back ,so give it a function.

$("#fooContainer").find(".foo").each( function(k, div){ operateOnFoo(div) } );

2 Comments

You missed the part in the question where operateOnFoo() wants the element passed as the first argument to it. As you have it, the .each index will be passed as the first argument to operateOnFoo().
Arh, yes confused with angular which first argument is value. edited
0

Considering fooContainer to be a class and not the ID of the container. You can use it like so:

$(".fooContainer .foo").each(function(index, element){
    operateOnFoo(element);
});

4 Comments

Why did you change fooContainer into a class when the OP specifies it as an id?
Who says fooContainer is an id? It is clearly mentioned in the question as "div of classname fooContainer"
The OP is using "#fooContainter" in three places which leads one to believe they are using an id..., but they don't show their HTML so it isn't 100% clear, I guess.
@jfriend00 maybe you are correct but as the OP said that he/she is new in web development, so I guessed that his syntax would be wrong.
0

Try this:

$("#fooContainer .foo").each(function(index, element){

     //Your code goes here...
     alert("Foo index: " + index + "\nContent: " + element);

});

Comments

0

Try this $(".fooContainter .foo").each(function (i, foo) { operateOnFoo( foo )}; );

The $(".fooContainter .foo") will select all the elements with class foo under fooContainer

2 Comments

Where does that foo variable come from?
I updated the answer to include the index and element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.