agente_secreto
agente_secreto

Reputation: 8079

How to write a jQuery function that performs an action through a series of elements, one at a time?

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.

Upvotes: 1

Views: 98

Answers (3)

user113716
user113716

Reputation: 322582

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);

Upvotes: 2

Chris Almond
Chris Almond

Reputation: 578

$('em').each(function(){

    // do stuff
});

Upvotes: 1

Pointy
Pointy

Reputation: 413966

$('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).

Upvotes: 4

Related Questions