<?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>Forem: Terrel Shumway</title>
    <description>The latest articles on Forem by Terrel Shumway (@terrelcodes).</description>
    <link>https://forem.com/terrelcodes</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%2F2929663%2F1011b294-321e-49a4-9d85-06a9e26d7c2c.jpeg</url>
      <title>Forem: Terrel Shumway</title>
      <link>https://forem.com/terrelcodes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/terrelcodes"/>
    <language>en</language>
    <item>
      <title>use onSubmit, not onClick</title>
      <dc:creator>Terrel Shumway</dc:creator>
      <pubDate>Fri, 25 Jul 2025 22:33:53 +0000</pubDate>
      <link>https://forem.com/terrelcodes/use-onsubmit-not-onclick-1493</link>
      <guid>https://forem.com/terrelcodes/use-onsubmit-not-onclick-1493</guid>
      <description>&lt;p&gt;The &lt;a href="https://www.solidjs.com/tutorial/stores_nested_reactivity" rel="noopener noreferrer"&gt;SolidJS tutorial&lt;/a&gt; has several pages that work with a To Do list app. This is the form used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;
    &amp;lt;input ref={input} /&amp;gt;
    &amp;lt;button onClick={(e) =&amp;gt; {
        if (!input.value.trim()) return;
        addTodo(input.value);
        input.value = "";
        }}
&amp;gt;Add Todo&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are doing your own To Do list (or any app that has a form with a single input field) you should do this instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form onSubmit={(e) =&amp;gt; {
    e.preventDefault()
    if (!input.value.trim()) return;
    addTodo(input.value);
    input.value = "";
    }}&amp;gt;
    &amp;lt;input autofocus ref={input} /&amp;gt;
    &amp;lt;button&amp;gt;Add Todo&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Putting the action in the onClick handler forces the user to click the button.&lt;br&gt;
Putting the action in the onSubmit handler allows the user to just press &lt;code&gt;Enter&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;autofocus&lt;/code&gt; magically returns focus to the input, nicer than an imperative &lt;code&gt;input.focus()&lt;/code&gt;.&lt;br&gt;
Also consider &lt;code&gt;e.target.reset()&lt;/code&gt; over &lt;code&gt;input.value=""&lt;/code&gt;.&lt;/p&gt;

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