Why formaction attribute is not working outside of <form> tag?

HTMLWeb DevelopmentFront End Technology

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>
raja
Published on 24-Jan-2018 05:28:25
Advertisements