316

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

8 Answers 8

574

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

$('#parentid').children('.childclass').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 () { /* ... */ });
4
  • 79
    then use $(this) within the closure to access the "current" item in the loop. Jun 11, 2010 at 16:15
  • 1
    @amarsuperstar: was just in the process of adding that information :-)
    – Andy E
    Jun 11, 2010 at 16:17
  • Is there a way to know the value of "n", assuming $(this) is the "n"th child of the parent? Sep 21, 2016 at 16:15
  • 1
    @SouvikGhosh: the index is passed as the first argument to the callback function for each(). Check the docs, linked in the answer above.
    – Andy E
    Sep 22, 2016 at 8:33
71

It is also possible to iterate through all elements within a specific context, no mattter how deeply nested they are:

$('input', $('#mydiv')).each(function () {
    console.log($(this)); //log every element found to console output
});

The second parameter $('#mydiv') which is passed to the jQuery 'input' Selector is the context. In this case the each() clause will iterate through all input elements within the #mydiv container, even if they are not direct children of #mydiv.

4
  • 1
    Probably because of nesting this solution worked for me whilst the other one did not. For that reason I would think that this is normally the better solution.
    – arame3333
    Oct 28, 2015 at 7:56
  • This is what I was looking for. Any way to make json from their values? I need to post all of theme as json.
    – Saqib
    Jun 19, 2018 at 6:35
  • Works great! What if I need all input AND select items though? Jun 7, 2021 at 13:13
  • You can select multiple elements through a comma separated list: $('input,select', $('#mydiv'))
    – Liquinaut
    Aug 2, 2021 at 14:05
22

If you need to loop through child elements recursively:

function recursiveEach($element){
    $element.children().each(function () {
        var $currentElement = $(this);
        // Show element
        console.info($currentElement);
        // Show events handlers of current element
        console.info($currentElement.data('events'));
        // Loop her children
        recursiveEach($currentElement);
    });
}

// Parent div
recursiveEach($("#div"));   

NOTE: In this example I show the events handlers registered with an object.

0
15
$('#myDiv').children().each( (index, element) => {
    console.log(index);     // children's index
    console.log(element);   // children's element
 });

This iterates through all the children and their element with index value can be accessed separately using element and index respectively.

8

It can be done this way as well:

$('input', '#div').each(function () {
    console.log($(this)); //log every element found to console output
});
3

I don't think that you need to use each(), you can use standard for loop

var children = $element.children().not(".pb-sortable-placeholder");
for (var i = 0; i < children.length; i++) {
    var currentChild = children.eq(i);
    // whatever logic you want
    var oldPosition = currentChild.data("position");
}

this way you can have the standard for loop features like break and continue works by default

also, the debugging will be easier

1
  • 1
    My experience is that $.each() is always slower than a for loop, and this is the only answer that uses it. The key here is to use the .eq() to access the actual element within the children array and not bracket ([]) notation.
    – elPastor
    Feb 19, 2019 at 14:58
1

children() is a loop in itself.

$('.element').children().animate({
'opacity':'0'
});
-1

It's working with .attr('value'), for elements attributes

$("#element div").each(function() {
   $(this).attr('value')
});
1
  • this is a 1:1 repeat of the answer from @UmarAsghar which is 4 years older
    – Henrik
    Mar 16, 2022 at 7:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.