Ver Mensaje Individual
  #2 (permalink)  
Antiguo 09/02/2013, 14:21
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 10 meses
Puntos: 1567
Respuesta: Problema con eventos en la creación de inputs dinámicos

El problema basicamente es que ese this.id que pasás en las funciones no existe, poruqe se refiere al id del elemento (input) creado

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <title>titulo</title>
  5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6.  
  7. <script type="text/javascript">
  8. //<![CDATA[
  9. var icremento =0;
  10. function crear(obj)
  11. {
  12. icremento++;
  13.  
  14. field = document.getElementById('field');
  15. contenedor = document.createElement('div');
  16. contenedor.id = 'div'+icremento;
  17. field.appendChild(contenedor);
  18.  
  19. boton = document.createElement('input');
  20. boton.type = 'text';
  21. boton.name = 'text'+'[]';
  22. boton.style.border = "1px solid white";
  23. boton.style.background = "#4F6497";
  24. boton.style.color = "white";
  25. boton.style.font = "x-small Arial";
  26. boton.id = "xxxxxxx_" + icremento;
  27. boton.onfocus = function () {estilo(this.id,'focus')};
  28. boton.onblur = function () {estilo(this.id,'blur')};
  29. //boton.onchange = function () {estilo(this.id,'change')};
  30. boton.onkeypress = function () {estilo(this.id,'keypress')};
  31. contenedor.appendChild(boton);
  32.  
  33. }
  34.  
  35. function estilo(a,b){
  36. document.getElementById('evento').innerHTML = a + '-' + b;
  37. }
  38. //]]>
  39. </head>
  40. <fieldset id="field" style="overflow: auto; height:70px; border:0px;"></fieldset>
  41.  
  42. <input type="button" value="Crear caja de texto" onclick="crear(this)" class="botones"><br />
  43. <div id="evento">
  44. </div>
  45. </body>
  46. </html>

fijate que previo a agregar los eventos definí
boton.id = "xxxxxxx_" + icremento;
el evento onchange, se va a sobreescribir con blur y focus asi no se si se justifica, menos si hay un keypress, pero bueno, ese es otro tema, ahora por lo menos los eventos deberían funcionarte

SAludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.