Yes,
You should add an event listener in input-1
of types
- KeyDown (when the key is pressed but does not release) or,
- KeyUp (when the key is released) or,
- KeyPress (when key is pressed)
- or Blur (when exiting the input)
And make this trigger a function that treats data from input-1
and insert them into input-2
.
Taking the following code as an example:
Javascript with JQuery to make it easier to view the binding of the event:
$(document).ready( function(){
$('#input-1').keyup(function (e) {
var patt = /([0-9]{1,2}:[0-9]{2})/;
// Testando se o valor digitado já é algo como nn:nn
if (patt.test(e.target.value)) {
var strsplit = e.target.value.split(':');
var hora = Number(strsplit[0]) + 2;
var min = (strsplit[1]);
var stringfinal = '';
// ex. se hora = 24 então vira 00, se hora = 25 então vira 01;
if (hora > 23) {
hora = hora - 24;
}
if (hora < 10) {
stringfinal += '0' + hora;
} else {
stringfinal += hora;
}
stringfinal += ':' + min;
$('#input-2').val(stringfinal);
} else {
// Faz nada...
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><inputid="input-1" type="text">
<input id="input-2" type="text">
This should give the planned effect, the function I did was pretty basic and can be greatly improved, it's more a proof of concept.
Events can be checked even without jQuery.
More information can be found at MDN Input and
I hope this answers your question.