I am constructing a dynamic HTML form and pre-fill the fields with data from a Javascript object like so:
function generatePersonalForm(data) {
html= '<form id="personalForm">'
html+='<label for="fname">First name:</label>'
html+='<input type="text" id="firstname" name="firstname" value="'+data.billing.contact.firstname+'"><br>'
html+='<label for="lname">Last name:</label>'
html+='<input type="text" id="lastname" name="lastname" value="'+data.billing.contact.lastname+'"><br>'
html+='<label for="lname">Data:</label>'
html+='<textarea id="emailbody" name="emailbody" rows="12" cols="50" value="'+data.emailbody+'"></textarea><br>'
html+='<input type="submit" value="Submit">'
html+='</form>'
return html;
}
I receive the data object from an API and afterwards I create the form and pre-fill it:
document.getElementById("personalData").innerHTML = generatePersonalForm(resp);
My Javascript object data.emailbody sometimes contains HTML (which is correct and intended!). However, if it contains a link like <a href=\"http://www.example.com\">www.example.com</a> the "(double quotes) in the object will break the form.
The same is with single quotes. Is there any option to show HTML content in a form value field dynamically added?