The reason we specify type="button" for button elements

Software engineer. Interested in small and efficient tech product teams and ambitious projects that move things forward.
Search for a command to run...

Software engineer. Interested in small and efficient tech product teams and ambitious projects that move things forward.
No comments yet. Be the first to comment.
Hello to anyone that might reach this website! Just want to let you know that I've changed the place where I publish and build new things. You can find things I'm up to on apisurf.dev. Cheers!
There are two good articles that touch on some of the concerns and annoyances I also share, but I'll try to cover some of my views about it. I had a chance to use Next.js 13 for a few weeks and noticed that some things make my job a lot simpler, whil...
new Date('YYYY-MM-DD') might not do what you think it does

Recently I stumbled upon an interesting test case where I needed to cover many possibilities and make sure everything continues to work as intended in the future. I had 7 boolean flags that needed to be used to toggle an app functionality. It's a bit...

Some old methods of accessing uploaded file contents inside a Next.js route handler don't work anymore or at least not in a way many tutorials explain it. E.g. many outdated online examples use page router which has route handlers that receive differ...

If you didn't have experience with the web ~10 years or more ago it might be weird to use buttons on the web, especially when you don't want them to trigger form submits. You might have asked yourself: "Why do I need to specify button type to be a button?". Shouldn't that be the default behaviour?
<button type="button">text</button>
Historically, the primary purpose for buttons was submitting and resetting HTML forms. “submit” was always the default type since button elements were used mostly for that purpose, submitting forms.
And we used them like so:
<form action="/send" method="post">
<input placeholder="Enter your message" />
<button>Save</button>
</form>
Then as JS evolved and became more and more powerful, people started building dynamic interfaces and apps. HTML buttons needed to follow and mimic the behaviour we see in native apps. There was a new requirement to have them trigger various actions inside the app, even more so than submitting forms. But, at the same time web platform has always been backwards compatible. For that reason, changing the existing HTML element to default to type="button" was not an option.
type="button" was introduced to support "new" behaviour that avoids the default of submitting forms.