Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to call HTMX in a single html page

I’m new to HTMX and I have made this sample:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <script src="https://unpkg.com/htmx.org@1.8.4" integrity="sha384-wg5Y/JwF7VxGk4zLsJEcAojRtlVp1FKKdGy1qN+OMtdq72WRvX/EdRdqg/LOhYeV" crossorigin="anonymous"></script>

        <form>
            <button hx-get="https://google.com/">Click Me!</button>
        </form>
    </body>
</html>

So I tried loading the htmx script firstly and then used hx-get to make a GET request for button.

But now the problem is, it does not work out at all!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It’s like adding a button with no action and nothing also appears in the Console bar as js error!

So if you know how to call this htmx properly, please let me know..

Thanks.


UPDATE

I removed the <form> tags and it looks like the htmx is now calling but retrieves these errors:

enter image description here

How to call htmx properly

>Solution :

When you click the submit button, the JS runs but you also trigger submission of the form.

Since Ajax is asynchronous, the form submits before the Ajax is complete. The browser then navigates to a new copy of the same page (killing the existing script, as a side effect, before the Ajax request completes).

Remove the form element from the document. You aren’t using it for anything.


Your next problem is that Google does not give you permission to access it cross-origin.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading