jquery 3.3.1
I have the following html:
<div class="form-group col-xs-10 col-sm-4 col-md-4 col-lg-4">
<label for="first">First Name:</label>
<input type="first" field="FirstName" type="text" class="form-control validate" />
</div>
<div class="form-group col-xs-2 col-sm-2 col-md-2 col-lg-2">
<label for="mi">Middle Initial:</label>
<input type="mi" field="MI" type="text" class="form-control input-sm validate" />
</div>
<div class="form-group col-xs-10 col-sm-4 col-md-4 col-lg-4">
<label for="last">Last Name:</label>
<input type="last" field="LastName" type="text" class="form-control validate" />
</div>
<div class="clearfix"></div>
<div class="form-group col-xs-10 col-sm-4 col-md-4 col-lg-4">
<label for="address">Address:</label>
<input type="address" field="Address" type="text" class="form-control input-lg validate" />
</div>
When i focus into an input field with class validate, I want to find the next field with the class validate. It will not be a sibling of the current elelemnt.
This is my jquery code:
$('.validate').focus(function (e) {
let ele = $(this).parent().next('.form-group').find('.validate');
let attr = ele.attr('field');
console.log('next: ' + attr);
});
If the next element with class validate appears after i do clearfix:
<div class="clearfix"></div>
my element is undefined in my jquery code. So when i tab out of the input LastName, the next element i want is Address but i cant find it with the above code. If the next element with class validate is directly after the current element without clearfix, it works:
<div class="form-group col-xs-2 col-sm-2 col-md-2 col-lg-2">
<label for="mi">Middle Initial:</label>
<input type="mi" field="MI" type="text" class="form-control input-sm validate" />
</div>
<div class="form-group col-xs-10 col-sm-4 col-md-4 col-lg-4">
<label for="last">Last Name:</label>
<input type="last" field="LastName" type="text" class="form-control validate" />
</div>
I tab out of Firstname and i can find input Middle Initial just fine. How do i get the the next element with class validate regardless of where it is located on the DOM tree?
Thanks