How do I iterate through children elements of a div using jQuery?

Shamoon picture Shamoon · Jun 11, 2010 · Viewed 386.7k times · Source

I have a div and it has several input elements in it... I'd like to iterate through each of those elements. Ideas?

Answer

Andy E picture Andy E · Jun 11, 2010

Use children() and each(), you can optionally pass a selector to children

$('#mydiv').children('input').each(function () {
    alert(this.value); // "this" is the current element in the loop
});

You could also just use the immediate child selector:

$('#mydiv > input').each(function () { /* ... */ });