Yes,
You should add a Event Listener in the input-1
of types
- Keydown (when the key is pressed but not released) 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 handles the data from input-1
and insert them into input-2
.
Taking the example of the following code:
Javascript with Jquery to make it easy to view 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>
<input id="input-1" type="text">
<input id="input-2" type="text">
This should give the planned effect, the function I did was very basic and can be greatly improved, is more a proof of concept.
Events can be measured even without jQuery.
More information can be found at Input on the MDN and Keyupevent at MDN
I hope this answers your question.
Dude. What a basic scene and I p'rali with
match
to the side. Bravo :)– MoshMage
How do I put a zero before, from 1 am to 9 am?
– thecreator
+1 btw, the
input
Value is type="teStbox" and it’s still working. So it should also work with type="text" (which it picks up by default) just right? Do you know if it is possible to use the same example for type="time" (with those dual time boxes, HTML5), or only if the input is text? Anyway thanks, it’ll be useful to me.– gustavox
@gustavox works with type="text" and type="time". It takes the input value, regardless of type. I edited the answer with an example of time.
– Randrade
Very good, will be very useful, thanks!
– gustavox