JavaScript - Añadir evento (addEventListener) a un elemento usando POO

 
Vista:
sin imagen de perfil

Añadir evento (addEventListener) a un elemento usando POO

Publicado por Ignacio (5 intervenciones) el 11/06/2017 13:34:51
Estoy intentando añadir un escuchador a un elemento input, que está creado de forma dinámica. Pero al indicar una función (método) de la clase donde lo creo cuando ocurre un evento (pulsar una tecla), esta función no se ejecuta cuando el evento se produce. Lo que ocurre al cargarse la página, es que la función se ejecuta una vez ¿Alguien sabe como se le puede dar a un elemento un método de la clase cuando ocurre un evento?

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
<!HTML doctype>
<html>
<head>
	<title>Asistencias</title>
</head>
<body>
	<div id = "contenedorNumeros"></div>
 
	<script>
		function Contenedor ( idContendor )
		{
			// ATRIBUTOS
 
			this.contenedor = document.getElementById ( idContendor );
			this.idElemento = 0;
			this.numeroElementos = this.contenedor.childNodes.length;
 
			// MÉTODOS
 
			this.crearInput = function () {
				// Crear atributos del nuevo elemento input
				var input = document.createElement ( 'input' );
				input.setAttribute ( "type", "text" );
				input.setAttribute ( "id", this.idElemento );
 
				// Añadir input al contenedor
				this.contenedor.appendChild ( input );
 
				// Añadir evento cuando se pulsa una tecla
				input.addEventListener ( 'keyup', this.comprobarNumeroEscrito( input ) );
 
				this.IdElemento++;
			}
 
			this.comprobarNumeroEscrito = function ( elemento ) {
				console.log ( "escribiendo" );
			}
		} // Fin clase Contenedor
 
		var contenedor = new Contenedor ( "contenedorNumeros" );
		contenedor.crearInput();
	</script>
</body>
</html>
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