Use XHR with jQuery slim, or even pure JS to handle form submission #410
VincentTam
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Motivation
Some developers want their site/theme independent of jQuery, for example, Hugo Swift Theme that I have been invited to collaborate. Others might consider using jQuery slim as many personal blogs need a short loading time rather than fancy animations.
Raison d'être
Each site/theme has its own UI and the script for handling of form submission varies from theme to theme.
Pure JS approach
@onweru has done a great work to parse the form data into JSON to be sent to the API instance. I've tried analyzing his work but I lack the confidence to port that to elsewhere.
https://github.com/onweru/hugo-swift-theme/blob/ef6bc3b099c7448806c13dbd52c2d198e0a8c43b/assets/js/index.js#L283-L299
fetch
in his code method doesn't support IE.jQuery slim compatible
Implementation:
user
,repository
andgitProvider
).The only place that uses jQuery slim is
$(this).serialize()
. Stack Overflow might show you other JS ways replace this function—I find the$
select extremely convenient to use, so I'll stick with jQuery slim, which provides theserialize
method.👨💻 I composed the names of the custom functions
formSubmitted
andformError
. Feel free to change them.Beta Was this translation helpful? Give feedback.
All reactions