_hyperscript icon indicating copy to clipboard operation
_hyperscript copied to clipboard

Hyperscript behavior ceases to work after active search

Open andrewah64 opened this issue 5 months ago • 2 comments

I'm using hyperscript 0.9.14. I have a table of data in a form, with the first column in each row being a checkbox. There is a behavior defined in a .hs file and installed on the

tag. Users can filter the contents of the table through an active search feature. This is implemented in HTMX and it swaps the tag out for new HTML based on the results of the search. The issue is that the behavior reacts to events correctly on the page's first load, but ceases to work following a search.

Is there a way to reassert a behavior following a change to a page's DOM?

<form hx-swap="none" _="install CheckBoxDataGrid">
	<table>
		<thead>
			<tr>
				<th>
					My checkbox
				</th>
			</tr>
		</thead>
		<tbody id="key-aur-inf-res"> <!-- active search swaps this out -->
			<tr>
				<td>
					<input type="checkbox" name="key-aur-mod-aauk-id" value="261">
				</td>
			</tr>
		</tbody>
	</table>
</form>
behavior CheckBoxDataGrid
	on click from <input[name="key-aur-mod-aauk-id"] /> in me
		log 'clicked checkbox'
	end
end

I've put a video of this below. On the first page load, the "on click" event handler fires, and button's text on the bottom right corner of the screen changes when checkboxes are selected. However, following an active search the event handler doesn't fire and the text doesn't change.

https://github.com/user-attachments/assets/9be8ebd0-5913-41c2-acc5-cfa7e2f1978c

andrewah64 avatar Aug 22 '25 16:08 andrewah64

I realised that by installing the behavior on the tbody element, this problem seems to go away. However, I think the question of how one can reassert/reload a behavior when a change occurs to the elements that it applies to still stands, as my form leverages infinite scrolling: i.e the number of records in the table will increase as the user scrolls down.

When this happens the behavior is applied (correctly) to the rows that were loaded in when either the screen was refreshed, or a search was instigated, but is not applied to rows added through infinite scrolling.

andrewah64 avatar Aug 22 '25 18:08 andrewah64

You basically have to reprocess the nodes that were added via AJAX(infinite scrolling). The node on which your behaviour is placed doesn't get replaced, so you'd have to "reinstall" the behaviour on htmx:afterSwap or other events. Have a look at this: https://hyperscript.org/docs/#install

In the last few sentences it says this:

If your HTML is received via AJAX or generated by scripts after initial page load, you need to call _hyperscript.processNode() to process the scripts on the new elements.

element.appendChild(newContent); _hyperscript.processNode(newContent); You can also call it from hyperscript itself

call _hyperscript.processNode(#new_element_id) Elements added by the put command are processed automatically; no need to call this function. Htmx does the same for all processed fragments.

sanchawebo avatar Nov 25 '25 13:11 sanchawebo