JavaScript - Getting HTML form values
HTML:
<input type="text" name="name" id="uniqueID" value="value" />
JS:
var nameValue = document.getElementById("uniqueID").value;
If you want to retrieve the form values (such as those that would be sent using an HTTP POST) you can use:
JavaScript
const formData = new FormData(document.querySelector('form'))
for (var pair of formData.entries()) {
// console.log(pair[0] + ': ' + pair[1]);
}
form-serialize (https://code.google.com/archive/p/form-serialize/)
serialize(document.forms[0]);
jQuery
$("form").serializeArray()
Here is an example from W3Schools:
function myFunction() {
var elements = document.getElementById("myForm").elements;
var obj ={};
for(var i = 0 ; i < elements.length ; i++){
var item = elements.item(i);
obj[item.name] = item.value;
}
document.getElementById("demo").innerHTML = JSON.stringify(obj);
}
The demo can be found here.