<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Alpinejs on Haseeb Majid</title>
    <link>https://haseebmajid.dev/tags/alpinejs/</link>
    <description>Recent content in Alpinejs on Haseeb Majid</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <lastBuildDate>Tue, 29 Apr 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://haseebmajid.dev/tags/alpinejs/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>TIL - How to Set Dynamic URL With HTMX and AlpineJS</title>
      <link>https://haseebmajid.dev/posts/2025-04-29-til-set-dynamic-url-with-htmx-and-alpinejs/</link>
      <pubDate>Tue, 29 Apr 2025 00:00:00 +0000</pubDate>
      
      <guid>https://haseebmajid.dev/posts/2025-04-29-til-set-dynamic-url-with-htmx-and-alpinejs/</guid>
      <description>&lt;p&gt;TLDR; Add this attribute to your x-bind:hx-delete &lt;code&gt;x-effect=&amp;quot;currentItemId;htmx.process($el)&amp;quot;&lt;/code&gt; as an example (adjust for your example).&lt;/p&gt;
&lt;h2 id=&#34;background&#34;&gt;Background&lt;/h2&gt;
&lt;p&gt;In my app, I show the user 25 feedbacks per page, and they can delete them which opens a modal to confirm the action.
If the user presses the delete button in the modal I want to send a HTTP DELETE request to an endpoint like
&lt;code&gt;/feedback/{id}&lt;/code&gt;, but the ID is dynamic, as we are using one modal for all the feedbacks vs having one modal
per one feedback.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>TLDR; Add this attribute to your x-bind:hx-delete <code>x-effect=&quot;currentItemId;htmx.process($el)&quot;</code> as an example (adjust for your example).</p>
<h2 id="background">Background</h2>
<p>In my app, I show the user 25 feedbacks per page, and they can delete them which opens a modal to confirm the action.
If the user presses the delete button in the modal I want to send a HTTP DELETE request to an endpoint like
<code>/feedback/{id}</code>, but the ID is dynamic, as we are using one modal for all the feedbacks vs having one modal
per one feedback.</p>
<p>I am using HTMX, generated using templ and using AlpineJS for some frontend interactivity, where it doesn&rsquo;t make
sense to send a request back to the server.</p>
<h2 id="solution">Solution</h2>
<p>The solution is relatively easily in the end</p>
<p>We have a button like this which triggers the modal to open, this is rendered in templ via Go. Hence the <code>fmt.Sprintf</code>
function. Here we are setting a variable which will be used by AlpineJS.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="line"><span class="cl"><span class="p">&lt;</span><span class="nt">button</span>
</span></span><span class="line"><span class="cl">    <span class="na">class</span><span class="o">=</span><span class="s">&#34;btn btn-ghost btn-xs text-neutral&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="err">@</span><span class="na">click</span><span class="o">=</span><span class="s">{</span> <span class="na">fmt</span><span class="err">.</span><span class="na">Sprintf</span><span class="err">(&#34;</span><span class="na">currentItemId </span><span class="o">=</span> <span class="s">&#39;%s&#39;</span><span class="err">;</span> <span class="na">document</span><span class="err">.</span><span class="na">getElementById</span><span class="err">(&#39;</span><span class="na">delete-modal</span><span class="err">&#39;).</span><span class="na">showModal</span><span class="err">()&#34;,</span> <span class="na">item</span><span class="err">.</span><span class="na">ID</span><span class="err">)</span> <span class="err">}</span>
</span></span><span class="line"><span class="cl">    <span class="na">aria-label</span><span class="o">=</span><span class="s">&#34;Delete Feedback&#34;</span>
</span></span><span class="line"><span class="cl"><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="p">&lt;</span><span class="nt">i</span> <span class="na">class</span><span class="o">=</span><span class="s">&#34;text-lg hgi hgi-solid hgi-delete-02&#34;</span><span class="p">&gt;&lt;/</span><span class="nt">i</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"><span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
</span></span></code></pre></div><p>In one of the parent div make sure we set the <code>x-data</code> attribute, which matches the same variable we set on the <code>@click</code>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="line"><span class="cl"><span class="p">&lt;</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">&#34;&#34;</span> <span class="na">x-data</span><span class="o">=</span><span class="s">&#34;{ currentItemId: &#39;&#39; }&#34;</span><span class="p">&gt;</span>
</span></span></code></pre></div><p>Finally here is a simplified version of the button in the modal when pressed will send the delete request.
The key line is <code>x-effect=&quot;currentItemId;htmx.process($el)&quot;</code>, we need HTMX to reevaluate the hx-delete attribute.
Else the request will go to <code>/feedback/</code> without the ID.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-html" data-lang="html"><span class="line"><span class="cl"><span class="p">&lt;</span><span class="nt">button</span>
</span></span><span class="line"><span class="cl">    <span class="na">x-bind:hx-delete</span><span class="o">=</span><span class="s">&#34;&#39;/feedback/&#39; + currentItemId&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="na">x-effect</span><span class="o">=</span><span class="s">&#34;currentItemId;htmx.process($el)&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="na">hx-target</span><span class="o">=</span><span class="s">&#34;#feedback_list&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="na">hx-swap</span><span class="o">=</span><span class="s">&#34;outerHTML&#34;</span>
</span></span><span class="line"><span class="cl"><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl">    Delete
</span></span><span class="line"><span class="cl"><span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
</span></span></code></pre></div><p>That&rsquo;s it, now you can set the endpoint dynamically, generally speaking I don&rsquo;t need to use this pattern much.
As most of the time this can be set when we render the template as they are usually static.</p>
<h2 id="appendix">Appendix</h2>
<ul>
<li>Github: <a href="https://github.com/mvolkmann/htmx-examples/blob/main/dynamic-endpoint/public/index.html">https://github.com/mvolkmann/htmx-examples/blob/main/dynamic-endpoint/public/index.html</a></li>
<li>Reddit Post: <a href="https://old.reddit.com/r/htmx/comments/1fyaw8r/htmx_and_alpinejs_dynamic_data/">https://old.reddit.com/r/htmx/comments/1fyaw8r/htmx_and_alpinejs_dynamic_data/</a></li>
</ul>
]]></content:encoded>
    </item>
    
  </channel>
</rss>
