Are button HTML tags outside of a form valid?


This article will teach us that button HTML tags outside of a form are valid. Before we get started, let's talk a little about the HTML <button> tag.

HTML <button> tag

An HTML <button> is made using the <button> element. Between the opening and closing tags, any text will be shown as text on the button.

Syntax

Following is the syntax for HTML<button> tag

<button type= “..”></button>

Anywhere in the document body where text-level markup is allowed, a button element is acceptable. such an element is not required to be related to a form element. Although a button element has a unique default rendering, It is best to only utilise buttons for items that are intended to perform a specific action when clicked.

A button element outside of a form has type=button as its default value. This indicates that JavaScript is the only way for it to function. However, rather than having them as static HTML text, you may think about creating such buttons using JavaScript.

Following are the examples for HTML button tags outside of a form are valid.

Example

In the following example we are creating the button outside the form by using formid and div.

<!DOCTYPE html> 
<html> 
<body> 
   <div> 
      <form id="my tutorial" action="https://www.tutorialspoint.com/index.htm"> 
         <label for="fname">NAME:</label> 
         <input type="text" name="fname"></input> 
      </form> 
      <button type="submit" form="my tutorial">SUBMIT</button>
   </div> 
</body> 
</html>

When the script gets executed, it will generate the output consisting of an input field for the name along with a submit button. When you click on the submit button, the form gets submitted to the mentioned form action.

Updated on: 15-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements