| 1 | --- |
| 2 | includeMockServer: true |
| 3 | title: "Linked Selects" |
| 4 | description: Update select options via another select |
| 5 | icon: "icon-[mdi--form-dropdown]" |
| 6 | --- |
| 7 | |
| 8 | <script> |
| 9 | function detailCard(make, modelName) { |
| 10 | const makes = { |
| 11 | audi: [ |
| 12 | { name: "A4", type: "Sedan", price: "$39,900" }, |
| 13 | { name: "Q5", type: "SUV", price: "$45,600" }, |
| 14 | { name: "e-tron GT", type: "Electric", price: "$106,500" }, |
| 15 | ], |
| 16 | toyota: [ |
| 17 | { name: "Tacoma", type: "Truck", price: "$31,500" }, |
| 18 | { name: "GR Supra", type: "Sport", price: "$56,250" }, |
| 19 | { name: "Land Cruiser", type: "SUV", price: "$58,250" }, |
| 20 | ], |
| 21 | bmw: [ |
| 22 | { name: "M3", type: "Sedan", price: "$76,000" }, |
| 23 | { name: "X5", type: "SUV", price: "$65,200" }, |
| 24 | { name: "i4", type: "Electric", price: "$52,200" }, |
| 25 | ], |
| 26 | }; |
| 27 | const brandName = { audi: "Audi", toyota: "Toyota", bmw: "BMW" }; |
| 28 | const selectCls = "w-full py-2.5 px-3 pr-8 rounded-md border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-900 text-sm font-medium text-neutral-800 dark:text-neutral-200 appearance-none cursor-pointer"; |
| 29 | const models = makes[make] || makes.audi; |
| 30 | const m = models.find(x => x.name === modelName) || models[0]; |
| 31 | return ` |
| 32 | <div class="starting:opacity-0 starting:translate-y-1 transition duration-300 ease-out flex items-center justify-between px-4 py-3 rounded-md bg-neutral-50 dark:bg-neutral-900 border border-neutral-100 dark:border-neutral-850"> |
| 33 | <div> |
| 34 | <div class="text-sm font-semibold text-neutral-800 dark:text-neutral-200">${brandName[make] || "Audi"} ${m.name}</div> |
| 35 | <span class="inline-flex items-center mt-1 px-1.5 py-0.5 rounded text-[0.5625rem] font-medium uppercase tracking-wider bg-neutral-200/50 dark:bg-neutral-800 text-neutral-500 dark:text-neutral-400">${m.type}</span> |
| 36 | </div> |
| 37 | <div class="text-right"> |
| 38 | <div class="text-sm font-semibold text-neutral-800 dark:text-neutral-200">${m.price}</div> |
| 39 | <div class="text-[0.625rem] uppercase tracking-wide text-neutral-450 dark:text-neutral-500 mt-0.5">MSRP</div> |
| 40 | </div> |
| 41 | </div>`; |
| 42 | } |
| 43 | |
| 44 | function modelSelect(make) { |
| 45 | const makes = { |
| 46 | audi: [ |
| 47 | { name: "A4", type: "Sedan", price: "$39,900" }, |
| 48 | { name: "Q5", type: "SUV", price: "$45,600" }, |
| 49 | { name: "e-tron GT", type: "Electric", price: "$106,500" }, |
| 50 | ], |
| 51 | toyota: [ |
| 52 | { name: "Tacoma", type: "Truck", price: "$31,500" }, |
| 53 | { name: "GR Supra", type: "Sport", price: "$56,250" }, |
| 54 | { name: "Land Cruiser", type: "SUV", price: "$58,250" }, |
| 55 | ], |
| 56 | bmw: [ |
| 57 | { name: "M3", type: "Sedan", price: "$76,000" }, |
| 58 | { name: "X5", type: "SUV", price: "$65,200" }, |
| 59 | { name: "i4", type: "Electric", price: "$52,200" }, |
| 60 | ], |
| 61 | }; |
| 62 | const selectCls = "w-full py-2.5 px-3 pr-8 rounded-md border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-900 text-sm font-medium text-neutral-800 dark:text-neutral-200 appearance-none cursor-pointer"; |
| 63 | const models = makes[make] || makes.audi; |
| 64 | return ` |
| 65 | <label class="block text-xs font-semibold uppercase tracking-wide text-neutral-450 dark:text-neutral-400 mb-2">Model</label> |
| 66 | <div class="relative starting:opacity-0 transition duration-300 ease-out"> |
| 67 | <select id="models" name="model" class="${selectCls}" |
| 68 | hx-get="/detail" |
| 69 | hx-target="#detail" |
| 70 | hx-include="[name='make']"> |
| 71 | ${models.map(m => `<option value="${m.name}">${m.name}</option>`).join("\n")} |
| 72 | </select> |
| 73 | <span class="absolute right-3 top-1/2 -translate-y-1/2 text-neutral-400 dark:text-neutral-500 pointer-events-none text-[0.625rem]">▾</span> |
| 74 | </div>`; |
| 75 | } |
| 76 | |
| 77 | server.get("/demo", () => { |
| 78 | const selectCls = "w-full py-2.5 px-3 pr-8 rounded-md border border-neutral-200 dark:border-neutral-700 bg-white dark:bg-neutral-900 text-sm font-medium text-neutral-800 dark:text-neutral-200 appearance-none cursor-pointer"; |
| 79 | return ` |
| 80 | <div> |
| 81 | <div class="grid grid-cols-2 gap-4"> |
| 82 | <div> |
| 83 | <label class="block text-xs font-semibold uppercase tracking-wide text-neutral-450 dark:text-neutral-400 mb-2">Make</label> |
| 84 | <div class="relative"> |
| 85 | <select name="make" class="${selectCls}" |
| 86 | hx-get="/models" |
| 87 | hx-target="#model-panel"> |
| 88 | <option value="audi">Audi</option> |
| 89 | <option value="toyota">Toyota</option> |
| 90 | <option value="bmw">BMW</option> |
| 91 | </select> |
| 92 | <span class="absolute right-3 top-1/2 -translate-y-1/2 text-neutral-400 dark:text-neutral-500 pointer-events-none text-[0.625rem]">▾</span> |
| 93 | </div> |
| 94 | </div> |
| 95 | <div id="model-panel"> |
| 96 | ${modelSelect("audi")} |
| 97 | </div> |
| 98 | </div> |
| 99 | <div id="detail" class="mt-3"> |
| 100 | ${detailCard("audi", "A4")} |
| 101 | </div> |
| 102 | </div>`; |
| 103 | }); |
| 104 | |
| 105 | server.get(/models.*/, (req) => { |
| 106 | const makes = { audi: [{name:"A4"},{name:"Q5"},{name:"e-tron GT"}], toyota: [{name:"Tacoma"},{name:"GR Supra"},{name:"Land Cruiser"}], bmw: [{name:"M3"},{name:"X5"},{name:"i4"}] }; |
| 107 | const make = req.params["make"]; |
| 108 | const first = (makes[make] || makes.audi)[0].name; |
| 109 | return modelSelect(make) + |
| 110 | `<div id="detail" hx-swap-oob="true" class="mt-3">${detailCard(make, first)}</div>`; |
| 111 | }); |
| 112 | |
| 113 | server.get(/detail.*/, (req) => detailCard(req.params["make"], req.params["model"])); |
| 114 | |
| 115 | server.start("/demo"); |
| 116 | </script> |
| 117 | |
| 118 | <div id="demo-content" class="not-prose demo-container min-h-[240px]"></div> |
| 119 | |
| 120 | ## Basic usage |
| 121 | |
| 122 | On the client, set the first select to request new options whenever its value changes. |
| 123 | |
| 124 | ```html |
| 125 | <select name="make" |
| 126 | hx-get="/models" |
| 127 | hx-target="#models" |
| 128 | hx-indicator="#models-indicator"> |
| 129 | <option value="audi">Audi</option> |
| 130 | <option value="toyota">Toyota</option> |
| 131 | <option value="bmw">BMW</option> |
| 132 | </select> |
| 133 | |
| 134 | <select id="models" name="model"> |
| 135 | <option value="A4">A4</option> |
| 136 | ... |
| 137 | </select> |
| 138 | <span id="models-indicator" class="htmx-indicator">Loading...</span> |
| 139 | ``` |
| 140 | |
| 141 | - [`hx-get`](/reference/attributes/hx-get) requests `/models` with the current `make` value as a query parameter. |
| 142 | - [`hx-target`](/reference/attributes/hx-target)=`"#models"` swaps the response into the second select. |
| 143 | - [`hx-indicator`](/reference/attributes/hx-indicator)=`"#models-indicator"` shows a loading message while the request is in flight. |
| 144 | - No [`hx-trigger`](/reference/attributes/hx-trigger) is needed: htmx defaults to `change` for `<select>` elements. |
| 145 | |
| 146 | On the server, respond with a new set of `<option>` elements matching the selected make: |
| 147 | |
| 148 | ```html |
| 149 | <option value="M3">M3</option> |
| 150 | <option value="X5">X5</option> |
| 151 | <option value="i4">i4</option> |
| 152 | ``` |
| 153 | |
| 154 | Because the default [`hx-swap`](/reference/attributes/hx-swap) strategy is [`innerHTML`](/reference/attributes/hx-swap#innerhtml), the options replace the contents of the `#models` select. |