HTML button form attribute

2023-06-09 #html

Thanks to my colleague Sam I recently learned about the form attribute on the <button> element.

By setting a form attribute, the button becomes a submit button for a form on the page with that ID, without having to nest the button on the page.

This could be useful for a logout link, used on different places.

<nav>
<!---->
<button type="submit" form="logout">
Log out
</button>
</nav>
 
<footer>
<!---->
<button type="submit" form="logout">
Log out
</button>
</footer>
 
<form id="logout" method="POST" action="/logout">
</form>