No access to function with addEventListener

0

Making a call with javascript using addEventListener to a create user function, this function does not show the alert. Why are not you accessing the function? Thanks

formu.php

<form action="crear.php" method="post">
   <div class="campo">
      <label for="nombre">Nombre:</label>
      <input type="text" name="nombre" id="nombre" placeholder="Nombre">
   </div>
   <div class="campo">
      <label for="numero">Teléfono:</label>      
      <input type="text" name="numero" id="numero" placeholder="Número">
    </div>
    <input type="submit" value="Agregar" id="agregar" class="boton">  
</form>
<script src="js/app.js"></script>

the app.js

var agregarContacto = document.getElementById('agregar');

function crearUsuario(){
    alert("funciona");
}

agregarContacto.addEventListener('click', function(){
    crearUsuario();
});
    
asked by RicardoKra 04.05.2018 в 23:16
source

1 answer

0

What happens is that when you press the Add button, it redirects you to another page, create.php. To check if it is working you can add this:

agregarContacto.addEventListener('click', function(event){
    event.preventDefault();
    crearUsuario();
});
    
answered by 04.05.2018 в 23:21