<!DOCTYPE html>
<html>
<head></head>
<body>
<form method="POST" action="act.php">
<p>Click the button to create a DIV element with some text, and append it to DIV.</p>
<div id="myDIV">
MATHS PAPER
</div>
<button onclick="myFunction()">Add Question</button>
<input type="submit" value="Create"> </input>
<script>
function myFunction() {
var para = document.createElement("DIV");
para.innerHTML = "<div style='background-color:lightgreen'>QUESTION<div><input type='text' id='q1' placeholder='enter question'></div><div></input><input type='text' placeholder='enter option1 here'></input></div><div></input><input type='text' placeholder='enter option2 here'></input></div></div>";
document.getElementById("myDIV").appendChild(para);
}
</script>
</form>
</body>
</html>
How to make this code work? I observed on removing form tag, it is working. But I want it to be in form tag, so that I can post those questions and options, and save it in a database.
reference: https://www.w3schools.com/jsref/met_document_createelement.asp
type="button"for button element should solve your problem.