Using jQuery I can use the .serialize()
method to return in the form of string all the items of the form with their respective values, such as the form below, will return me:
nome=dvd&email=dvd%40dvd.com&sexo=1
Form:
$("form").on("submit", function(e){
e.preventDefault();
var form = $(this).serialize();
console.log(form);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><form><inputtype="text" name="nome" value="dvd" />
<br />
<input type="text" name="email" value="[email protected]" />
<br />
<input type="radio" name="sexo" value="1" checked> masculino
<input type="radio" name="sexo" value="2"> feminino
<br />
<input type="submit" value="Enviar" />
</form>
How could I get the same string (result of serialize()
of jQuery) but using pure JavaScript?
Consider that the form above is just a basic example. O form may have other elements, such as
select
,textarea
,button
etc.