How can i reload/refresh a form when user input's wrong as am generating form dynamically, I managed to display a warning message but having problem resetting the field, also suggest any improvements needed in my code?
...
JavaScript:
const div = document.querySelector(".addHere");
document.querySelector(".btn").addEventListener("click", addInputs);
function addInputs() {
const inputValue = parseInt(document.querySelector(".inputValue").value);
if (isNaN(inputValue)) {
alert("Wrong input");
const form = document.createElement("form");
form.method = "post";
form.action = "#";
} else {
for (let i = 1; i <= inputValue; i++) {
const input1 = document.createElement("input");
input1.type = "text";
input1.maxLength = "12";
input1.className = "factor";
input1.required = true;
const input2 = document.createElement("input");
input2.type = "text";
input2.maxLength = "1";
input2.className = "priority";
input2.required = true;
const br = document.createElement("br");
form.appendChild(br.cloneNode());
form.appendChild(input1);
form.appendChild(input2);
form.appendChild(br.cloneNode());
div.appendChild(form);
}
const sub = document.createElement("button");
sub.type = "submit";
sub.value = "Submit";
sub.className = "subButton";
sub.textContent = "Submit";
div.appendChild(sub);
}
}
HTML:
<div class="addHere"></div>
<div class="inputs">
<input type="text" maxlength="1" class="inputValue" placeholder="insert numbers:" />
<button class="btn">+</button>
</div>
You can reset the whole form's inputs to their original values like this:
<script>
document.getElementById('myform').reset();
</script>
Or only one input element like this:
<script>
document.getElementById('userName').value = '';
</script>
And if you want to reload the whole page, you should do this:
<script>
document.location.href = document.location.href;
</script>
<form id="myform">
<input id="userName" type="text" />
</form>
How about this?
CCS:
<style>
.subButton {
display: none;
}
</style>
Script:
<script>
window.addEventListener('load', (event) => {
document.querySelector(".btnAdd").addEventListener("click", addInputs);
});
function addInputs() {
const inputValue = parseInt(document.querySelector(".inputValue").value);
if (isNaN(inputValue) || inputValue < 1) {
alert("Wrong input");
document.querySelector(".inputValue").value = '';
} else {
let html = '';
for (let i = 1; i <= inputValue; i++) {
html += '<input type="text" maxlength="12" class="factor" required>';
html += '<input type="text" maxlength="1" class="priority" required>';
html += '<br />';
}
document.querySelector(".addHere").innerHTML = html;
document.querySelector(".subButton").style.display = 'block';
document.querySelector(".inputs").style.display = 'none';
}
}
</script>
HTML:
<body>
<form method="post" action="#">
<div class="addHere"></div>
<input type="submit" value="Submit" class="subButton" />
<div class="inputs">
<input type="text" maxlength="1" class="inputValue" placeholder="insert numbers:" />
<button type="button" class="btnAdd">+</button>
</div>
</form>
</body>