Im having trouble figuring out what the problem is here. It doesnt work when i use '#account-funding-'+i in the IF statement but does work with '#account-funding-0'. I need to be able to loop through the array of unknown amount of objects. What am i doing wrong?
HTML * There can be X number of .funding-content div's on the page. Every ID with a -0 will increment from the js
<form id="accountform" name="accountForm">
<div class="funding-content funding-selected-0">
<div class="container">
<h3>Funding for [account selected]</h3>
<div class="funding-type-transfer" id="funding-type-transfer-0">
<div class="form-inputs">
<div class="lx-grid lx-grid--gutters">
<div class="lx-grid__cell lx-grid__cell--1-2--above-md">
<label for="account-funding-0">Routing Number</label>
<input id="account-funding-0" type="text" maxlength="9">
</div>
</div>
</div>
</div>
</div>
</div>
</form>
Doesnt Work:
var amountOfAccounts = $( '#account-form .funding-content' ).length;
for ( i = 0; i <= amountOfAccounts; i++ ) {
if ( $( '#account-funding-'+i ).val() == '' ) {
document.getElementById( 'account-funding-'+i ).setCustomValidity( 'Routing Number is a required field.' );
}
}
Works:
var amountOfAccounts = $( '#account-form .funding-content' ).length;
for ( i = 0; i <= amountOfAccounts; i++ ) {
if ( $( '#account-funding-0' ).val() == '' ) {
document.getElementById( 'account-funding-'+i ).setCustomValidity( 'Routing Number is a required field.' );
}
}