tags:

views:

48

answers:

3

I am trying to write a function that applies an action through all <strong> elements, and when finishes with one, applies it to the next one.

Also, at the end, it must start again with the first <strong>.

I am sure I remember reading about a jquery function for doing this, but I cannot remember it.

+4  A: 
$('selector').each(function() {
  $(this).doSomething();
});

?? Upon each call to that function embedded there, "this" will refer to one of the elements selected by the given selector (like "input:checked" or whatever).

Pointy
thats what I meant, thank you. But i cant get it to work...My code is: $('#claim p strong').each(function() { $(this).animate({color: '#01245D'}, 500);});I know you usually cannot animate the color property, but I linked jquery ui and I've done in this site succesfully.
0al0
@0al0 - If Pointy's answer was what you meant, then what did you mean when you wrote *"at the end, it must start again with the first"* ?
patrick dw
@patrick Thats the second part of the problem, first I have to get .each() to work, then i will worry about making it loop...
0al0
@0al0 - My answer covers your entire question. See this example based on my answer: http://jsfiddle.net/34sFN/1/
patrick dw
@patrick You are right, you sir are a genius. But could someone please tell me why the code I posted before does not work? I just started learning jquery and would like to understand it...
0al0
@0al0 - Your code does work. http://jsfiddle.net/34sFN/5/ But if you're starting with black text, you won't notice much difference since the color you used is very dark. Also, subsequent loops will have no effect since the color is already set. What is the ultimate effect you're trying to achieve?
patrick dw
+2  A: 

For it to infinitely loop, you'll need to use something like setInterval() so it doesn't block other javascript from executing.

I assume that's what you mean when you say "Also, at the end, it must start again with the first again."

Here's an example: http://jsfiddle.net/34sFN/

Another example, somewhat based on your comment: http://jsfiddle.net/34sFN/1/

var $ems = $('em');

// Loops through all your elements every 100 milliseconds.
// Change the duration to suit.
setInterval(function() {
    $ems.each(function() {
        // Do something to the current EM using $(this)
    });
},100);
patrick dw
+1  A: 
$('em').each(function(){

    // do stuff
});
Chris Almond