<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jayesh Bhoot</title>
    <description>The latest articles on DEV Community by Jayesh Bhoot (@bhoot).</description>
    <link>https://dev.to/bhoot</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1838960%2F3d78aa70-5837-40f7-b945-730d80cdd89b.png</url>
      <title>DEV Community: Jayesh Bhoot</title>
      <link>https://dev.to/bhoot</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bhoot"/>
    <language>en</language>
    <item>
      <title>Use cases for &lt;form method="dialog"&gt;</title>
      <dc:creator>Jayesh Bhoot</dc:creator>
      <pubDate>Wed, 15 Jan 2025 10:52:16 +0000</pubDate>
      <link>https://dev.to/bhoot/use-cases-for-f0c</link>
      <guid>https://dev.to/bhoot/use-cases-for-f0c</guid>
      <description>&lt;p&gt;I've started using a relatively less known value of the &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; element's &lt;code&gt;method&lt;/code&gt; attribute, called &lt;code&gt;dialog&lt;/code&gt;, for the following scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;when a form is submitted via AJAX.&lt;/li&gt;
&lt;li&gt;when a form is a multi-step wizard, where each step is a form in itself, and I have to accummulate values from each step.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  But what about MDN talking about using it in a &lt;code&gt;&amp;lt;dialog&amp;gt;&lt;/code&gt; element?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form" rel="noopener noreferrer"&gt;MDN reference&lt;/a&gt; defines &lt;code&gt;method="dialog"&lt;/code&gt; as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When the form is inside a &lt;code&gt;&amp;lt;dialog&amp;gt;&lt;/code&gt;, closes the dialog and causes a submit event to be fired on submission, without submitting data or clearing the form.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But the HTML spec, having been written in officialese and being the more authoritative source than MDN, provides a nice escape hatch.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-method" rel="noopener noreferrer"&gt;The spec defines&lt;/a&gt; &lt;code&gt;method="dialog"&lt;/code&gt; as follows (emphasis mine):&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Indicates the form is intended to close the dialog box in which the form finds itself, &lt;em&gt;if any&lt;/em&gt;, and otherwise not submit.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;My interpretation is that &lt;code&gt;method="dialog"&lt;/code&gt; has two jobs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;If&lt;/em&gt; a &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; using &lt;code&gt;method="dialog"&lt;/code&gt; is located inside a &lt;code&gt;&amp;lt;dialog&amp;gt;&lt;/code&gt; element, then its submission causes the dialog to close.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;method="dialog"&lt;/code&gt; also does not really submit the form, i.e., it fires the submit event, but leaves the actual submission to further programming, like AJAX. As a nice side-effect, &lt;strong&gt;I don't have to fire an explicit &lt;code&gt;ev.preventDefault()&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Am I wrong? May be. Please let me know in the comments.&lt;/p&gt;

</description>
      <category>html</category>
    </item>
  </channel>
</rss>
