Ver Mensaje Individual
  #1 (permalink)  
Antiguo 04/05/2011, 08:50
omarMusic
 
Fecha de Ingreso: febrero-2011
Ubicación: Evolandia
Mensajes: 103
Antigüedad: 13 años, 2 meses
Puntos: 10
Agregar evento onsubmit dinámicamente

Holas a todos, tengo una duda cual es la forma correcta de agregar el evento onsubmit a un <form> dinamicamente o sea un equivalente a onsubmit="return verifica(this)", de la forma q a continuación la hago no me funciona correctamente

Html:

Cita:
<form action="prueba.php" method="post" id="loginForm">
...
</form>
Este es mi código JS

Código Javascript:
Ver original
  1. window.onload = function() {
  2.     addEvent("submit", "loginForm", function() {return verifica(this)})
  3. }
  4.  
  5. function verifica (form) {
  6.     with(form) {
  7.         if(usuario.value == "") {
  8.             alert("Llene el campo usuario");
  9.             return false;
  10.         }
  11.         if (password.value == "") {
  12.             alert("Llene el campo password");
  13.             return false;
  14.         }
  15.     }
  16.     return true;
  17. }
  18.  
  19. function addEvent(event, elem, func) {
  20.     elem = document.getElementById(elem)
  21.     if (elem.addEventListener) {  // W3C DOM
  22.         elem.addEventListener(event,func,false)
  23.         return true
  24.     } else
  25.         if (elem.attachEvent) // IE DOM
  26.             return elem.attachEvent("on"+event, func)
  27.         else {
  28.             throw 'No es posible añadir evento'
  29.             return false
  30.         }
  31. }

Gracias, espero sugerencias
Saludos!