Extract form data as a string in javascript

Extract form data as a string in javascript
0

#1

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!


#2

#3

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:

document.querySelector('[name="inputEmail"]')

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

document.querySelector('[name="inputEmail"]').value

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: [email protected]"

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


#4

Thank you both…