Why formaction attribute is not working outside of
tag?


We can make the formaction attribute to work outside the <form> tag. The formaction attribute is used to specify more than one submit URLs for one form. When you submit a form, the web browser first checks for a formaction attribute.

If the formaction is not present, the web browser moves on to look for an action attribute on the form element.

Example

Here’s an example of the formaction attribute with three different submit buttons −

<!DOCTYPE html>
<html>
   <head>
      <title>HTML formaction attribute</title>
   </head>

   <body>
      <form method="post">
         <input type = "text" name="name"/><br>
         <button type = "submit" formaction = "btn1.php">Button1</button>
         <button type = "submit" formaction = "btn2.php">Button2</button>
         <button type = "submit" formaction = "btn3.php">Button3</button>
      </form>
   </body>
</html>

Yes, the formaction attribute won’t work outside the form element, but you can still let them work correctly in the following way −

Example

You can easily place the button and use the formaction attribute outside the form, using the associated form id value.

<!DOCTYPE html>
<html>
   <head>
      <title>HTML formaction attribute</title>
   </head>

   <body>
      <form method="post" id="newForm">
         <input type="text" name="name"/>
      </form>

      <button type="submit" formaction="btn1.php" form="newForm">Button1</button>
      <button type="submit" formaction="btn2.php" form="newForm">Button2</button>
      <button type="submit" formaction="btn3.php" form="newForm">Button3</button>
   </body>
</html>

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 15-Jun-2020

521 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements