Guilherme Almeida
Guilherme Almeida

Reputation: 89

HTML Buttons behaves weirdly on click

So say I have this HTML code:

function sendMain() {
  swal({
    title: "Preparando para mandar Email...",
    text: "Clique em 'OK' para mandar o Email.",
    type: "info",
    showCancelButton: true,
    closeOnConfirm: false,
    showLoaderOnConfirm: true,
  }, function() {
    setTimeout(function() {
      swal("Redirecionando!");
    }, 2000);
  });
}
<button id="send" onclick="sendMain()">Mandar</button>

Where swal is the SweetAlert show alert function. The thing is: when I click it, it shows the message for 1/4 of second and then goes to the top of the page and adds "?" to the end of the page's URL. What is wrong?

Upvotes: 0

Views: 76

Answers (1)

nnnnnn
nnnnnn

Reputation: 150080

You didn't specify a type for your button, so by default it is a submit button. And the default behaviour for a submit button is to try to submit its form which makes the page reload. Add the following attribute:

type="button"

Then the button will have no default action and thus will only do whatever you specify via Javascript.

Upvotes: 2

Related Questions