Debian Codegraph @

www/src/content/reference/01-attributes/11-hx-vals.md

raw symbols SCIP
1 ---
2 title: "hx-vals"
3 description: "Adds values to request parameters"
4 ---
5
6 The `hx-vals` attribute adds values to the parameters that will be submitted with the request.
7
8 ## Syntax
9
10 ```html
11 <div hx-get="/search" hx-vals='{"category": "books"}'>
12 Search
13 </div>
14 ```
15
16 ## JSON Object
17
18 Provide a JSON object directly:
19
20 ```html
21 <button hx-post="/api/user" hx-vals='{"role": "admin", "active": true}'>
22 Create Admin
23 </button>
24 ```
25
26 ## JavaScript Expression
27
28 Use `js:` prefix to evaluate JavaScript:
29
30 ```html
31 <div hx-get="/search" hx-vals='js:{query: document.querySelector("#search").value}'>
32 Search
33 </div>
34 ```
35
36 > **Security warning:** When using the `js:` prefix, be aware that you are introducing security considerations, especially when dealing with user input such as query strings or user-generated content, which could introduce a Cross-Site Scripting (XSS) vulnerability.
37
38 ## The `:append` Modifier
39
40 By default, a child element's `hx-vals` declaration completely replaces the parent's value for any shared key. Use the `:append` modifier to merge the child's values into the parent's instead:
41
42 ```html
43 <div hx-vals='{"category": "books", "lang": "en"}'>
44 <!-- Replaces parent entirely for shared keys -->
45 <button hx-get="/search" hx-vals='{"category": "fiction"}'>Fiction</button>
46
47 <!-- Merges with parent, overriding only "category" -->
48 <button hx-get="/search" hx-vals:append='{"category": "fiction"}'>Fiction</button>
49 </div>
50 ```
51
52 ## Inheritance
53
54 A child element's `hx-vals` declaration completely replaces the parent value for any key that appears in both. Use the `:append` modifier to merge the values instead of replacing them.
55
56 ## Comparison with `hx-include`
57
58 - `hx-include` pulls values from existing input elements in the DOM
59 - `hx-vals` adds arbitrary static or computed values that don't correspond to a form element
60
61 Both attributes can be used together: `hx-include` captures input data while `hx-vals` supplies additional parameters.
62
63 ## Notes
64
65 * Values are added to the request body (POST, PUT, PATCH, DELETE) or query parameters (GET)
66 * If a value already exists in the form, `hx-vals` will override it
67 * The JavaScript expression is evaluated at request time
68 * Can return a Promise for async value resolution