convertir script a una funcion para llamarla luego onkeypress
Publicado por Eduardo (186 intervenciones) el 03/02/2021 18:50:58
Hola a todos tengo el siguiente código el cual hace que el campo al ir escribiendo una hora salga los : puntos automáticamente pero el script esta ligado al campo.. como hago para convertir el script en una función para llamar dicha función desde el campo al ir escribiendo tipo <input type="text" onkeypress="myFunction()">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<input name="hora1" type="text" required="required" id="hora1" maxlength="5" style="font-size:20px;" autocomplete="off" size="8" autofocus="autofocus"/>
<script>
document.getElementById("hora1").addEventListener("keypress", function(e) {if (this.value.length<5 && /[0-9]/.test(e.key)) {
this.value+=e.key.toUpperCase();
}
if (this.value.length>=5 && /[0-9]/.test(e.key)) {
return;
}
if (this.value.length==2) {
this.value+=":";
}
e.preventDefault();
});
</script>
Valora esta pregunta


0