I have created a page with a form for people to complete, which when completed produces a text file of the person’s answers.
However I want people to have the option of skipping a question if they don’t want to answer it.
The problem with the code below is that if you skip a question it still produces the preceding/following text and spaces.
i.e. if a person doesn’t want to answer the question about their age, the below code will still produce text which reads “Age:[space][space]” in the text file.
If someone skips a question how to I stop the text file still producing the above? Ideally I want it to be completely ignored in the text file if the question hasn’t been answered.
// Get the data from each element on the form.
const name = document.getElementById('txtName');
const age = document.getElementById('txtAge');
const email = document.getElementById('txtEmail');
const country = document.getElementById('selCountry');
const msg = document.getElementById('msg');
// This variable stores all the data.
let data =
'r Name: ' + name.value + ' rn ' +
'Age: ' +age.value + ' rn ' +
'Email: ' + email.value + ' rn ' +
'Country: ' + country.value + ' rn ' +
'Message: ' + msg.value;
// Convert the text to BLOB.
const textToBLOB = new Blob([data], { type: 'text/plain' });
const sFileName = 'formData.txt'; // The file to save the data.
let newLink = document.createElement("a");
newLink.download = sFileName;
if (window.webkitURL != null) {
newLink.href = window.webkitURL.createObjectURL(textToBLOB);
}
else {
newLink.href = window.URL.createObjectURL(textToBLOB);
newLink.style.display = "none";
document.body.appendChild(newLink);
}
newLink.click();