Extract form data as a string in javascript

Hello everyone,

How can I extract the form data as a string using vanilla javascript, that is to say a method like .serialize() in jQuery.
Any help is very appreciated.

Thanks in advance!

You will need to select the input elements. There are multiple ways of doing this, but the easiest I can think of for a form would be something like this:


You can then use the value property to get their data as a string:


You could then build a string using template literals:

let email = document.querySelector('[name="inputEmail"]').value
let name = document.querySelector('[name="firstname"]').value
let data = `FirstName: ${name}, Email: ${email}`

console.log(data) // "FirstName: John, Email: johndoe@duckduckgo.com"

This answer lists several other ways to get data from inputs:

Thank you both…