however, when i tried to get the values from the dynamically created form fields, i get null values from the form submission but not with the text fields created in html. Any help would be appreciated.
small code snippet.
for (i=0;i<number;i++){
// Append a node with a random text
container.appendChild(document.createTextNode(("Sl.No ")));
container.appendChild(document.createTextNode((i+1)));
// Create an <input> element, set its type and name
attributes
//name
var input = document.createElement("input");
input.type = "text";
input.name = "name" + i;
container.appendChild(input);
//age
var input = document.createElement("input");
input.type = "text";
input.name = "age" + i;
container.appendChild(input);
//book button
var input = document.createElement("input");
input.type = "submit";
input.name = "sbtn";
input.value="Book";
}
Below code gets dynamically created fields perfectly submitted.
HTML:
<form id="myForm" action="/" method="post">
<label id="authlabel">User Authentication method:</label>
<br>
<select id="authMethod" name="authmethod">
<option disabled selected value> -- select an option -- </option>
<option value="userName">Name</option>
<option value="userId">User ID</option>
</select>
<br>
<input type="submit" value="submit"></input>
</form>
JS:
const myForm = document.querySelector('#myForm');
document.querySelector('#authmethod').addEventListener('change', function(event){
let authInput = document.createElement('input');
authInput.name = event.target.value;
myForm.insertBefore(authInput, event.target.nextSibling);
});
If, for some reason, it doesn't work for you, would you submit your HTML and full javascript context?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.