Comments (4)
Hey, thanks for the question. The response must contain valid HTML and the two IDs that you're targeting.
So you could return the exact same HTML markup with a new comment and updated count like this:
<h2>Comments (<span id="comments_count">2</span>)</h2>
<ul id="comments">
<li>Comment #1</li>
<li>Comment #2</li>
</ul>
<h2 id="comment_form_title">Post a Comment</h2>
<form x-target="comments comments_count" method="post" action="/comment" aria-labelledby="comment_form_title">
<input name="comment" required />
<button>Submit</button>
</form>
or you could return only the elements you targeted:
<h2>Comments (<span id="comments_count">2</span>)</h2>
<ul id="comments">
<li>Comment #1</li>
<li>Comment #2</li>
</ul>
or you could return a complete HTML document:
<!DOCTYPE html>
<html>
<head>
<title>Post a comment</title>
</head>
<body>
<h1>Post a comment</h1>
<h2>Comments (<span id="comments_count">2</span>)</h2>
<ul id="comments">
<li>Comment #1</li>
<li>Comment #2</li>
</ul>
<h2 id="comment_form_title">Post a Comment</h2>
<form x-target="comments comments_count" method="post" action="/comment" aria-labelledby="comment_form_title">
<input name="comment" required />
<button>Submit</button>
</form>
</body>
</html>
All these options will work, the only thing that matters is that there is a id="comments"
element and a id="comments_count"
element in the response.
Let me know if that clears things up and I'll update the documentation with an example response.
from alpine-ajax.
Hi @imacrayon
I prefer this way, then the response txt would be both together right?
So x-target would separately place two elements in right target anchor, that is what I am cuious.
If the response txt is just for one element, and target is one location anchor, I do not have this question.
I am just curious about the detail whether need to split them, or if no, how it can place the right element from many elements to right anchor.
The tutorial just mentioned it has this multiple target feature.
Thanks for your help. Waiting for your instructions.
Eric Xin
from alpine-ajax.
how it can place the right element from many elements to right anchor.
All the elements have to exist on the page, before the request so that Alpine AJAX knows where to place them. So if you use x-target="comments comments_count"
and your HTML looks like this (no comments_count
element):
<h2>Comments (0)</h2>
<ul id="comments">
</ul>
<form x-target="comments comments_count">...</form>
You'll get an error in the console because id="comments_count"
doesn't exist on the page.
![Screenshot 2023-06-19 at 12 37 15 PM](https://private-user-images.githubusercontent.com/3410149/246898524-e91cb26e-2e74-4a7d-9772-af1085c1283d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTI3NTgxNjYsIm5iZiI6MTcxMjc1Nzg2NiwicGF0aCI6Ii8zNDEwMTQ5LzI0Njg5ODUyNC1lOTFjYjI2ZS0yZTc0LTRhN2QtOTc3Mi1hZjEwODVjMTI4M2QucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDQxMCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA0MTBUMTQwNDI2WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YjkyNWJhYWYxMjgyZGVmOTkwM2IyM2Y5NWRmMGRiZDIxNTMyZTAzMWQ3MmEzZDNkZTA3YzRlYjM5NTZiN2RiMyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.pnRg8PDZZb3zKFJiA-oZu3sIPCpezdkiVWEJ6YWc4tY)
Here's a CodePen of the comment form you can explore
from alpine-ajax.
Thanks so much, so the library would handle the ID anchor respectively, so I can render all elements with different ID, then rest of them the library would handle.
from alpine-ajax.
Related Issues (20)
- Document x-alpine-request header
- Make a GitHub Action build step
- Title and meta tags don't updated HOT 3
- Can't self target on `<form>` when it has `x-merge` HOT 3
- `$ajax` on `<form method=GET>` send formData instead of queryParams HOT 6
- Question on passing CSRF tags for Django HOT 1
- [BUG] x-merge=prepend,append,update does not seem to work HOT 3
- x-ref replace x-target HOT 15
- Bug when target is a tr
- TypeError in latest version HOT 2
- Able to set `enctype` to application/x-www-form-urlencoded HOT 1
- Add support for `autofocus`
- `MissingIdError` error thrown when using `x-target.replace` without an ID HOT 2
- Calling $ajax from code outside of Alpine.js HOT 5
- Use ajax:success to preload assets (js) HOT 3
- Could not find a declaration file for module '@imacrayon/alpine-ajax' HOT 3
- Any way to make incoming html replace the innerHTML of target instead of the whole target? HOT 2
- Uncaught (in promise) Missing Target: #[object HTMLButtonElement] was not found in the current document. HOT 5
- SSE and websockets support HOT 3
- Using x-morph, new Alpine components don't initialize HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alpine-ajax.