Answers.Body: <p>If I understand correctly, Sprig is still an option, so long as you can localize the trigger + swap behavior. Try this out!</p>
  <p>In your homepage template, add the like button, targeting a custom route/path that includes a query param with the current post’s ID:</p>
  <pre><code>{% set featured = craft.entries()
    .section('blog')
    .featured(true)
    .all() %}
  
  &lt;ul&gt;
    {% for post in featured %}
      &lt;li&gt;
        {{ post.title }}
        &lt;button
          class=&quot;like like--inactive&quot;
          s-trigger=&quot;click&quot;
          s-get=&quot;{{ url('ajax/like', { postId: post.id }) }}&quot;
          s-swap=&quot;outerHTML&quot;&gt;Like&lt;/button&gt;
      &lt;/li&gt;
    {% endfor %}
  &lt;/ul&gt;
  </code></pre>
  <p>Then, in a new template located at <code>templates/ajax/like.twig</code>, add this:</p>
  <pre><code>{% set target = craft.entries()
    .section('posts')
    .id(craft.app.request.getRequiredQueryParam('postId'))
    .one() %}
  
  {# Bail if the user provided a bogus ID: #}
  {% if not target %}
    {% exit 404 %}
  {% endif %}
  
  {# Increment the post’s “like” sequence (this key isn’t important—it just needs to be unique to each post): #}
  {% set likes = seq(&quot;post-#{target.id}:likes&quot;) %}
  
  &lt;div class=&quot;like like--active&quot;&gt;
    Liked {{ likes }} time(s)
  &lt;/div&gt;
  </code></pre>
  <p>There isn't anything particularly special about this—it's just a template that renders an HTML fragment, and is only intended to be swapped out via Sprig. Its context (the post entry) is loaded dynamically based on the incoming <code>postId</code> query parameter.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-02-23T20:40:17.433Z
Answers.LastActivityDate: 2023-02-23T20:40:17.433Z
Answers.UserDisplayName: August Miller
Body: <p>I think this is the last issue that I have on my dev site before going live.</p>
  <p>In <code>index.twig</code> I'm outputting the latest articles, some of them are duplicated.</p>
  <p>In the footer I'm having <code>popper.js</code> tooltip where on click of a “LIKE” button I can update the tooltip to have information about <code>entry.id</code></p>
  <p>Now I have to do <code>seq(‘likes’ next=true)</code> on that entry via AJAX and that's where I need help.</p>
  <p>I can't do this via Sprig because it's the whole homepage.</p>
  <p>Thanks in advance.</p>
  
Tags: <ajax>
Title: Ajax increment seq
ThreadText: # Ajax increment seq
  
  (Asked by: username_0 on 2023-02-23)
  
  I think this is the last issue that I have on my dev site before going live.
  
  In `index.twig` I'm outputting the latest articles, some of them are duplicated.
  
  In the footer I'm having `popper.js` tooltip where on click of a “LIKE” button I can update the tooltip to have information about `entry.id`
  
  Now I have to do `seq(‘likes’ next=true)` on that entry via AJAX and that's where I need help.
  
  I can't do this via Sprig because it's the whole homepage.
  
  Thanks in advance.
  
  **Tags:** ajax
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-02-23. Score: 2
  
  If I understand correctly, Sprig is still an option, so long as you can localize the trigger + swap behavior. Try this out!
  
  In your homepage template, add the like button, targeting a custom route/path that includes a query param with the current post’s ID:
  
  ```
  {% set featured = craft.entries()
    .section('blog')
    .featured(true)
    .all() %}
  
  <ul>
    {% for post in featured %}
      <li>
        {{ post.title }}
        <button
          class="like like--inactive"
          s-trigger="click"
          s-get="{{ url('ajax/like', { postId: post.id }) }}"
          s-swap="outerHTML">Like</button>
      </li>
    {% endfor %}
  </ul>
  
  ```
  
  Then, in a new template located at `templates/ajax/like.twig`, add this:
  
  ```
  {% set target = craft.entries()
    .section('posts')
    .id(craft.app.request.getRequiredQueryParam('postId'))
    .one() %}
  
  {# Bail if the user provided a bogus ID: #}
  {% if not target %}
    {% exit 404 %}
  {% endif %}
  
  {# Increment the post’s “like” sequence (this key isn’t important—it just needs to be unique to each post): #}
  {% set likes = seq("post-#{target.id}:likes") %}
  
  <div class="like like--active">
    Liked {{ likes }} time(s)
  </div>
  
  ```
  
  There isn't anything particularly special about this—it's just a template that renders an HTML fragment, and is only intended to be swapped out via Sprig. Its context (the post entry) is loaded dynamically based on the incoming `postId` query parameter.
  
  
  ---
  
  