Can we use .each inside .each in jquery?

$("[name=form_1]").each(function(){ alert("i in else"+i); $('.eform_text').each(function() { }); }); 

whether this cycle will be repeated over all elements that have the eform_text class only in form1. Or will it iterate over all elements having this class?

Update:

The exact jsp code is as follows:

<c:when test="${eformDetails.controlType==1}"> <input id="textBox_${eformDetails.id}_${eformDetails.required}_${i}" class="eformDetail eform_text" type="text" value="" name="form_${i}" onblur="validateEformInputs(${i-1})"></input> </c:when>

I have a form that changes every time. And for each form I need to get all the text fields. Currently, after your help, my javascript looks like this:

$ ("[name = form _" + i + "]"). Each (function (s) {alert ("i in else" + i);

  $('.eform_text', this).each(function() { textboxId = $(this).attr("id"); 

He reaches the first warning, but I cannot reach the second cycle. It does not receive elements that have the eform_text class. Not sure what is going on here. Could you help me?

+6
source share
3 answers

It will iterate over all elements with this class, whether inside the form with the name "form_1" or not. To look only in each form (I assume you should have several forms named "form_1", ​​although this seems odd), use find in the outer loop to cover the inner loop:

 $("[name=form_1]").each(function(formIndex) { alert("formIndex in each: " + formIndex); $(this).find('.eform_text').each(function(textIndex) { alert("textIndex in each: " + textIndex); }); }); 

Or you can use the second argument to $() , which provides the context for the job:

 $("[name=form_1]").each(function(formIndex) { alert("formIndex in each: " + formIndex); $('.eform_text', this).each(function(textIndex) { alert("textIndex in each: " + textIndex); }); }); 

Or should work.

Please note that as @Shrikant Sharat noted in the comments (thanks to Shrikant!), I assumed that the i in your source code is for the index that is passed to each . I showed indexes at both levels (with descriptive names) above.

+11
source

Your second answer.

Since you call $( every time, it creates a new copy of the jQuery object that does not care about what level of function it has.

It will go through each element with this class.

+1
source
 $('.element').each(function(){ $(this).find('.elementChild').each(function(){ // do something }); }); 
0
source

Source: https://habr.com/ru/post/888338/


All Articles