I'm looking to setup a standard DOB question where, once the user satisfies the maxlength
requirement, it will automatically focus onto the next input (being the month field).
However, my current approach doesn't autofocus onto the next field?
$(function() {
$(".input").keyup(function() {
if (this.value.length == this.maxLength) {
console.log(this.maxLength);
var $next = $(this).next('.input');
if ($next.length)
$(this).next('.input').focus();
else
$(this).blur();
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="question question--1">
<input class="input" type="number" data-format="day" placeholder="DD" maxlength="2" />
</div>
<div class="question question--2">
<input class="input" type="number" data-format="month" placeholder="MM" maxlength="2" />
</div>
<div class="question question--3">
<input class="input" type="number" data-format="year" placeholder="YYYY" maxlength="4" />
</div>
I believe the issue might be because the next .input
is in another parent element. I've tried tweaking it to the following:
var $next = $(this).parent(".question").next('.input');
But still the same results.