Is it possible using Javascipt
to automatically send the user from one field to the next when the maxlength
of the field has been reached?
If so, how?
Is it possible using Javascipt
to automatically send the user from one field to the next when the maxlength
of the field has been reached?
If so, how?
yes it is possible. Suppose your textbox max length is 5. You need to make an function onkeyup event and count the length of the textbox value. In this function if length is equal or exceed to 5 then you need to write second textbox focus function calls.
$('#productkey1').keyup(function() {
if(this.value.length >= $(this).attr('maxlength'))
{
$('#productkey2').next().focus();
}
});
Yes, you need to calculate the number of charecter inserted in the field by onkeyup
event and if the number is equal to maxlength then set the focus
to next field
you can use code like this using jquery. In this code it will move to #second field when text is larger then 20 characters
<input id="first"/>
<input id="second"/>
<script>
$(function() {
$('#first').keypress(function() {
var self = $(this);
//wait until character is inserted
setTimeout(function() {
if (self.val().length > 20) {
$('#second').focus();
}
}, 1);
});
});
</script>
First you need to hook into the onfocus
event for the input to know you are starting the measuring. Then you need to check the length of the entered characters with the onkeyup
event. Once your limit is reached you can call focus()
on the next DOM element you want to move the cursor to. You will also need to use the onblur
event to know that you have stopped measuring the length of an input.
For example, using jQuery;
<input id="inputA" type="text"/>
<input id="inputB" type="text"/>
<script>
var $input;
$('#inputA').focus(function() {
$input = $(this);
});
$(window).keyup(function() {
if($input && $input.val().length == 5) {
$('#inputB').focus();
}
});
$('#inputA').blur(function() {
$input = null;
});
</script>