views:

31

answers:

1

Basically, is there a way to 'cycle' through child elements.

i.e.:

  • some text
  • some more text
  • even more text

I want to get the text from the first '<'li'>', perform a few operations, then move on to the next one and next one until I have gone through all of the child elements. Is this possible?

+2  A: 

Your question could use a little more explanation, but jQuery's .each() is what I think you are looking for. Not to by confused with jQuery.each, which you can use to iterate over any collection, .each iterates over all of the elements that are within the jQuery object created from your selector expression. It executes a callback function for each element that is part of the jQuery object. The callback function is passed the index of the element and the element itself. It's important to note that the callback is fired in the context of the current DOM element, so this refers to the element, and is not a jQuery object. So you could do something like:

$("selectorForListElem").children().each(function(index, currentElem) {
    // processing for current child element
});

I've created a very simple example over on jsFiddle.

Alex
Great help man. Exactly as I needed it. A little overboard with the example but if I was a total noob I would have been infinitely thankful for it.
Glenn Nelson
sorry about that, didn't know how in-depth to go. Glad I could be of help.
Alex