user
11/25/2021, 3:05 AMhx-preserve. It shouldn't matter if its being done as part of htmx load polling or anything like that?limited-diamond-93357
11/25/2021, 11:29 AMlimited-diamond-93357
11/25/2021, 11:30 AMlimited-diamond-93357
11/25/2021, 11:30 AMlimited-diamond-93357
11/25/2021, 11:30 AMbland-coat-6833
11/25/2021, 11:44 AMlimited-diamond-93357
11/25/2021, 11:55 AMgreen-addition-61042
11/25/2021, 12:29 PMuser
11/26/2021, 4:33 AMhx-preserve? Struggling to understand exactly how it works in context of swapping HTML. Everytime I swap a page with a valid hx-preserve element, it is reloaded.bland-coat-6833
11/26/2021, 6:54 AMuser
11/26/2021, 10:03 AM<div hx-post="/preserve_content/click" hx-trigger="every 3s">
<p>Refresh # <%= session[:counter] %></p>
<iframe hx-preserve='true' id='iframe1' src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
I grabbed the <iframe> example from [the htmx website](https://htmx.org/attributes/hx-preserve/)
Here is my Rails controller:
class PreserveContentController < ApplicationController
# DEVELOPMENT PURPOSES ONLY
skip_before_action :verify_authenticity_token
def click
session[:counter] = 0 unless session[:counter]
session[:counter] += 1
puts "counter is #{session[:counter]}"
render partial: 'refresh'
end
endmost-jelly-15242
11/26/2021, 11:55 AM<div id="test" hx-preserve="true">foo</div> that I manually change before it's replaced and that one works. I then tested the iframe example and it also didn't work for me. I am still on v1.5.0. If you are on the latest version and still doesn't work there, then this might be a bug. I also tested wrapping the iframe in a div with hx-preserve and a unique id and that also didn't work.stocky-sundown-96428
11/26/2021, 1:23 PMuser
11/26/2021, 2:59 PMuser
11/26/2021, 3:03 PM<iframe> with a Bootstrap spinner which seems to work perfectly.
<div hx-post="/preserve_content/click" hx-trigger="every 3s">
<p>Refresh # <%= session[:counter] %></p>
<div id="my-spinner" hx-preserve="true" class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
Thanks @User I could kiss you!most-jelly-15242
11/26/2021, 3:04 PMhx-preserve on an input I use for search so it doesn't get replaced while typing in it, and that also doesn't work.user
11/26/2021, 3:06 PMdiv with an hx-preserve?most-jelly-15242
11/26/2021, 3:06 PMbland-coat-6833
11/26/2021, 3:19 PMmysterious-toddler-20573
11/26/2021, 3:20 PMmysterious-toddler-20573
11/26/2021, 3:20 PMmysterious-toddler-20573
11/26/2021, 3:21 PMlate-king-98305
11/26/2021, 9:30 PMicy-thailand-88996
11/27/2021, 5:52 AMmost-jelly-15242
11/27/2021, 8:42 PMfrom: and body. I think it's using the space as part of the selector.
html
<div hx-get="/tickets" hx-target="this" hx-trigger="click, customEvent from:body" hx-swap="afterend">
2. You can use the htmx Javascript API to simplify the vanilla JS code:
js
htmx.trigger(htmx.find('body'),'customEvent')
or
js
htmx.trigger('body','customEvent')icy-thailand-88996
11/28/2021, 4:38 AMbody = document.querySelector('body')
htmx.trigger('body','customEvent')salmon-xylophone-28580
11/28/2021, 2:29 PMmysterious-toddler-20573
11/28/2021, 7:55 PMkind-lizard-34601
11/29/2021, 1:02 PMmysterious-toddler-20573
11/29/2021, 2:08 PM