js get form data code example

Example 1: jquery get form data

$('form').serializeArray()
// gives 
// 	[ {"name":"foo","value":"1"},
//  {"name":"bar","value":"xxx"},
//  {"name":"this","value":"hi"} ]


// or 

$('form').serialize() // gives : "foo=1&bar=xxx&this=hi"

Example 2: javascript get form data

<html>
    <form id="myForm">
        <input type="text" name="email" value="[email protected]">
    </form>
    <p id='text'></p>

    <script>
        window.setInterval(()=>{
            var myForm = document.getElementById('myForm');
            var text = document.getElementById('text');

            text.innerText = myForm.elements['email'].value;
        }, 1);
    </script>
</html>

Example 3: javascript form data

var formData = new FormData();
fromData.append('name','value');

Example 4: js get data from form

const form = document.querySelector('form');
const data = new FormData(form);

Example 5: javascript form data

var formData = new FormData();

Example 6: js get form data

document.addEventListener('submit', function (event) {

	event.preventDefault();

	fetch('https://xxx.xxx', {
		method: 'POST',
		body: new FormData(event.target),
	}).then(function (response) {
		if (response.ok) {
			return response.json();
		}
		return Promise.reject(response);
	}).then(function (data) {
		console.log(data);
	})
});

Tags: