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

How can I grab a query string value and apply it on load? #524

Closed
cbmeeks opened this issue Mar 26, 2024 · 5 comments
Closed

How can I grab a query string value and apply it on load? #524

cbmeeks opened this issue Mar 26, 2024 · 5 comments

Comments

@cbmeeks
Copy link

cbmeeks commented Mar 26, 2024

I have an input box. What I would like to do is set the value of it on page load from the URL query parameter.

For example, in JS I would do this:

const input = document.getElementById('myInput')
input.value = new URLSearchParams(window.location.search).get('myQueryParam')

However, I can't seem to figure out how to do this in HTMX and/or Hyperscript.

Any ideas how I can do this?

Thanks!

@WedersonCD
Copy link

WedersonCD commented Mar 26, 2024

You could do something like this:
<input type="text" _=" init js return new URLSearchParams(window.location.search).get('parameter') end set @value to result end "/>

@cbmeeks
Copy link
Author

cbmeeks commented Mar 26, 2024

Oh my word I looked forever and couldn't find that.

Good news is that I almost had that myself but messed up the "set @value to result" part..

Thanks!!!

@cbmeeks cbmeeks closed this as completed Mar 26, 2024
@cbmeeks
Copy link
Author

cbmeeks commented Mar 26, 2024

It seems this doesn't work with a select box.

                    <select class="w-80" id="documentStandard" name="documentStandard"
                            aria-label="Document Standard"
                            th:hx-get="|@{/document-types/standards}|"
                            hx-trigger="change"
                            hx-target="#dt-list"
                            hx-swap="innerHTML"
                            _="init js return new URLSearchParams(window.location.search).get('documentStandard') end set @value to result end" >
                        <option value=""></option>
                        <option value="EDIFACT">EDIFACT</option>
                        <option value="X12">X12</option>
                    </select>

I can log the "value" and it works. I'm thinking there is some sort of race condition. Also, I have another select box that does the same thing but the "options" are the result of a slow hx-get query.

Any tips on this one?

Thanks

@cbmeeks cbmeeks reopened this Mar 26, 2024
@WedersonCD
Copy link

you didn't control the default select option using the value property.

You can use the selected property into the option.

` <select class="w-80" id="documentStandard" name="documentStandard"
aria-label="Document Standard"
hx-get="|@{/document-types/standards}|"
hx-trigger="change"
hx-target="#dt-list"
hx-swap="innerHTML"
_="init
js
return new URLSearchParams(window.location.search).get('documentStandard')
end
set defaultValue to result
add @selected to <option[value='${defaultValue}']/>

end" >

EDIFACT
X12
`

@cbmeeks
Copy link
Author

cbmeeks commented Mar 26, 2024

Oh, cool. Thanks.

I also discovered this works but not sure how/why.

_="init js return new URLSearchParams(window.location.search).get('documentStandard') end set <select[name='documentStandard']/>'s value to result end"

@cbmeeks cbmeeks closed this as completed Mar 26, 2024
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

No branches or pull requests

2 participants