Skip to content

serialize.js let's you serialize forms without further libraries and with support for all browsers that are currently supported by their manufacturer, meaning everything above IE 5.5 will run this without any problems

License

lucakiebel/serialize.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

serialize.js

INSTALL | USAGE | AUTHOR | LICENSE

This is a modification of the Google Project form-serialize. Click the link to find the original piece of software.

window.location.href = "https://example.com/?"+serialize(document.forms[0]);

MOTIVATION

It's still very common not to include any heavy libraries into a JavaScript project. This tries to solve the problem of not being able to serialize forms in browsers that don't support the FormData API.

INSTALLATION

Include the minified script in your HTML, this will expose the function serialize():

<script src="https://content.luca-kiebel.de/libraries/serialize/serialize.min.js"></script>
<form id="test">
  <input type="text" name="some" value="Random">
  <input type="text" name="field" value="Values"> 
  <input type="number" name="names" value="123565">
  <input type="password" name="andAPassword" value="andMore">
  <br><br>
  <button type="button" id="serialize">Serialize!</button>
</form>
<hr>
<p id="s">Result: </p>
<script>
document.getElementById("serialize").onclick = () => {
  document.getElementById("s").innerHTML += (serialize(document.forms[0])); 
}
</script>

See it in action at CodePen

USAGE

In most cases, forms need to be serialized to send their data to an API endpoint. Using the fetch() API, this is how you would go about accomplishing this:

<script src="https://content.luca-kiebel.de/libraries/serialize/serialize.min.js"></script>
<form>
	<input type="text" name="ID">
	<input type="text" name="Name"> <br>
	<button type="submit">Submit</button>
</form>
<script> 
  document.forms[0].onsubmit = () => { 
    let url = "https://yourDomain.tld/end/point";
    let data = serialize(document.forms[0]); 
    fetch(url + "?" + data)
      .then(function(response) {
        return response.json();
      })
      .then(function(myJson) {
        console.log(myJson);
      });
    return false; 
  } 
</script>

AUTHOR

Original Auhtor of Google Project: biggie4Life

LICENSE

MIT License

Copyright (c) 2018 Luca Kiebel

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

PRODUCTION STATUS & SUPPORT

You should be aware that this project is supported solely by me and provided as is.


Go back to the project description

About

serialize.js let's you serialize forms without further libraries and with support for all browsers that are currently supported by their manufacturer, meaning everything above IE 5.5 will run this without any problems

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published