JavaScript - Codigo que lo haga sin oprimir boton

 
Vista:
Imágen de perfil de Eduardo
Val: 159
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Codigo que lo haga sin oprimir boton

Publicado por Eduardo (176 intervenciones) el 30/01/2020 15:02:26
tengo este código que muy amablemente me pasaron..

1
2
3
4
5
6
7
8
9
10
11
12
<button onclick="test()">Test</button>
 
<input type="text" id="demo">
 
<script>
function test() {
var str = document.getElementById("demo").value;
if(isNaN(str))return;
document.getElementById("demo").value =
str.slice(0,1) + ":" + str.substr(1,4);
}
</script>

El cual si yo escribo en el campo por ejemplo 123 y al oprimir el botón me lo divide así 1:23 (como si fueran horas y minutos)

como hago para que a medida que escribo lo haga automático y no tener que oprimir el botón...

mil gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Eduardo
Val: 159
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Codigo que lo haga sin oprimir boton

Publicado por Eduardo (176 intervenciones) el 30/01/2020 16:15:38
Mil gracias.. de todas pondre el codigo mencionado aca para el que lo necesite

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Hora: <input type="text" class="hora" size="5" autofocus />
<script>
	document.querySelector('.hora').addEventListener('keypress',validateHour);
	document.querySelector('.hora').addEventListener('blur',formatHour);
 
	function validateHour(e){
		e.preventDefault();
		if ( !isNaN(e.key) || e.key==':' ) {
			var position = this.selectionStart;
			var text = this.value;
			var textStart = text.substr(0,position);
			var textEnd   = text.substr(this.selectionEnd);
 
			if ( (textStart+textEnd).search(':')!=-1 && e.key==':' ) return;
			var textNew = textStart + e.key + textEnd;
			textNew = textNew.replace(':','');
			if ( textNew.length>2 ){
				textStart = textNew.substr(0,textNew.length-2);
				textEnd   = textNew.substr(-2);
				textNew = textStart + ':' + textEnd;
				position++;
			} else if ( textNew>59 ) {
				textStart = textNew.substr(0,1);
				textEnd   = textNew.substr(1);
				textNew = textStart + ':' + textEnd;
				position++;
			}
 
			var textPart=textNew.split(':');
 
			if ( textPart.length == 2 && textPart[0]>23 || textPart[1]>59 ) return;
 
			this.value = textNew;
 
			this.selectionStart = position+1;
			this.selectionEnd = position+1;
		}
	}
 
	function formatHour(){
		var text = this.value;
		if ( text!="" ) {
			textPart = text.split(':');
 
			if ( textPart.length == 2 ) {
				textPart[0]=textPart[0].padStart(2,'0');
				textPart[1]=textPart[1].padStart(2,'0');
			} else {
				textPart.push(textPart[0].padStart(2,'0'));
				textPart[0]='00';
			}
			this.value=textPart.join(':');
		}
	}
</script>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
-1
Comentar