0

I just noticed this and am curious. I have two <span>s with onClick events that do basically the same thing; one had type='button', but the other did not. They were both behaving exactly the same.

Is there any point to adding type='button' to an element other than making it more clear what it is?

Even a standard button element seems to behave exactly the same with and without.

<button onclick="alert('Hello world!')">Click Me!</button>

&&

<button type="button" onclick="alert('Hello world!')">Click Me!</button>

ram
  • 680
  • 5
  • 15

1 Answers1

1

You can make a form to see the difference:

<form onsubmit="alert('submited...')">
  <button>Click me</button>
</form>

<form onsubmit="alert('submited...')">
  <button type="button">Click me</button>
</form>

A button tag without type attribute in the first example, it relates to the submit button (<button type="submit">Click me</button>) while the second button doesn't.

Tân
  • 1
  • 15
  • 56
  • 102