Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using htmx.ajax and hx-target #701

Open
Benoss opened this issue Dec 3, 2021 · 5 comments · Fixed by #1985
Open

Using htmx.ajax and hx-target #701

Benoss opened this issue Dec 3, 2021 · 5 comments · Fixed by #1985

Comments

@Benoss
Copy link

Benoss commented Dec 3, 2021

I am triggering htmx.ajax but my response contains a full page and I am trying to swap a part of the response
What I am doing is working but seems really silly is there a cleaner way to do it?

  function swap_htmx(target, something) {
    var doc = document.createElement('html');
    doc.innerHTML = something.xhr.responseText
    target.replaceWith(doc.querySelector("#inner-page"))
  }

htmx.ajax('GET', new_url, {source: "#inner-page", handler:swap_htmx})
window.history.pushState({}, '', new_url);

Another solution would be to configure properly the element "#inner-page" using the hx- markup and trigger an event, but I can't find a way to replace the value of hx-get. I used setAttribute but seems to be ignored

<div class="lg:w-98% w-75% flex flex-col gap-8 relative" id="inner-page" hx-trigger="filter-event" hx-target="this" hx-swap="outerHTML" hx-push-url="true">
old data
</div>

    let inner = htmx.find("#inner-page")
    inner.setAttribute("hx-get", new_url)
    htmx.trigger(inner, "filter-event")

Thanks for your help

@benpate
Copy link
Collaborator

benpate commented Dec 9, 2021

@Benoss -- Your second solution is the better way to go, with one small change. If you're dynamically changing the value of hx-get, you need to tell htmx about the change. Put htmx.process(node) at the end of your Javascript and it should update correctly.

@1cg -- Unless we're changing event handlers that are registered via hx-trigger I don't see why htmx couldn't just look up the action (hx-get, hx-post, hx-delete, hx-put) dynamically when the element is activated. It might even save a small amount of up-front processing. Is there a reason this needs to be scanned and cached ahead of time?

@1cg
Copy link
Contributor

1cg commented Jan 16, 2022

Agree w/ you ben, no reason to aggressively cache the request URL. But I think the crux here is that the htmx.ajax() function needs more functionality. We need to include a select option, just like we have a target option:

https://htmx.org/api/#ajax

I'll add it to the list

@applecat
Copy link

applecat commented Nov 8, 2022

@1cg, are there any plans to implement select option of htmx.ajax() in the near future? Definitely a useful thing, it would simplify the writing of a server-side HTML render function.

@mardix
Copy link

mardix commented Dec 21, 2022

Can we get the select select option added in htmx.ajax?

@ece-jacob-scott
Copy link
Contributor

ece-jacob-scott commented Nov 9, 2023

@1cg

I ran into this issue where I needed "select" on the ajax javascript function a couple days ago and wouldn't mind taking a crack at implementing it. Is it still on the list of things that need doing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants