Is there a way to get the resulting HTML of form fields where the user has entered data? For example, I only seem to be able to retrieve something like this using innerHTML:
<input type="text" />
<textarea></textarea>
But I would like to get something like this:
<input type="text" value="John Smith" />
<textarea>Hello! How are you today?</textarea>
Edit:
Here's code for a minimum reproducible example:
function go() {
alert(document.getElementById('example').innerHTML);
}
<div id=example>
<input type=text />
<textarea></textarea>
<input type=button value=go onclick="go()" />
</div>
Yes, you can get value from html form by using javascript. You can get value which user input in the form.
You have to declare id in input field or textarea field.
<input type="text" id="name" value="John Smith" />
<textarea id="name">Hello! How are you today?</textarea>
Now get the field by using javascript.
const input = document.getElementById("id")
Now to get value, just use value property of this element.
input.value
or you can use it directly
document.getElementById("id").value
For getting entire dump from a html form.
<form action="" method="post" id="BookPackageForm">
Then fetch the form element by using forms object.
var formEl = document.forms.BookPackageForm;
Get the data from the form by using FormData objects.
var formData = new FormData(formEl);
Get the value of the fields by the form data object.
var name = formData.get('name');