$("[name=form_1]").each(function(){
alert("i in else"+i);
$('.eform_text').each(function() {
});
});
would this loop iterate over all elements that have a class of eform_text only in form1 .Or would it iterate over all elements which have that class ?
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 the form which varies each time.and for each form i need to obtain all the text boxes.Currently after your help my javascript is ass follows:
$("[name=form_"+i+"]").each(function(i){ alert("i in else"+i);
$('.eform_text', this).each(function() {
textboxId = $(this).attr("id");
It reaches the first alert but i am not able to reach the second loop.It is not obtaining elements that have class eform_text.Not sure what is going wrong here.Could you please help?
To continue in a JavaScript forEach loop you can't use the continue statement because you will get an error. Instead you will need to use the return statement in place of the continue statement because it will act in the same way when using a forEach because you pass in a callback into the forEach statement.
Learn how to loop through elements, arrays and objects with jQuery using the $. each() function, jQuery's foreach equivalent. jQuery's foreach equivalent can be very useful for many situations. These examples will get you started and teach you how you can loop through arrays, objects and all kinds of HTML elements.
each(), which is used to iterate, exclusively, over a jQuery object. The $. each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.
each() loop at a particular iteration by making the callback function return false . Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration. Note that $(selector).
It would iterate over all elements with that class, whether inside a form with the name "form_1" or not. To only look within each form (I'm guessing you must have more than one form with the name "form_1", though that seems odd), use find
in the outer loop in order to scope 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 in which to work:
$("[name=form_1]").each(function(formIndex) {
alert("formIndex in each: " + formIndex);
$('.eform_text', this).each(function(textIndex) {
alert("textIndex in each: " + textIndex);
});
});
Either should work.
Note that as @Shrikant Sharat pointed out in the comments (thanks Shrikant!), I've assumed the i
in your original code is meant to be the index that gets passed into each
. I've shown the indexes at both levels (with descriptive names) above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With