Onclick message with javascript does not hold [closed]

3

I have the following code to show a message when I press a certain button, but the message disappears almost instantaneously and can not be read correctly:

function informacion() {
  document.getElementById("info").innerHTML = "Preferencias Actualizadas!";
}
<div class="text-center">
  <button onclick="informacion()" type="submit" value="submit" name="submit" class="btn btn-primary">Guardar</button>
</div>
<br>

<p class="text-center" id="info"></p>
    
asked by Alejandro 26.03.2018 в 18:42
source

3 answers

2

If you have no major problem in making modifications, do the following

  • remove the submit attribute and leave it like this
  • remove the call to the function and just put an id
  • Use the addEventListener to link the button with the click event
  • Here is a functional example that will leave the text visible

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    </head>
    <body>
    <div class="text-center">
      <button id="envio" class="btn btn-primary">Guardar</button>
    </div>
    <br>
    
    <p class="text-center" id="info"></p>
      <script>
        const envio = document.getElementById("envio")
        
        envio.addEventListener("click", function () {
          let aviso = document.getElementById("info")
          aviso.innerHTML = "Enviado correctamente"
        })
      </script>
    </body>
    </html>
        
    answered by 26.03.2018 в 20:00
    1

    The problem is that your button makes a submit when you click.

    Remove the attribute of type="submit" and take a test.

        
    answered by 26.03.2018 в 19:57
    1
        <html>
    <head>
    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js">
        </script>
    <head>
    <script>
     function myFunction() {
        swal("Good job!", "You clicked the button!", "success");
    }
    </script>
    <body>
      <button onclick="myFunction()">Click me</button>
    </body>
    </html>
    
        
    answered by 26.03.2018 в 20:06