I'm trying to create a a button to add a text field to a form. I am having a few issues. First, when I press the add choice button, it doesn't put the new text box under the last choice, but rather next to it. Also, I don't know how to add the text "Chocie:" before the text box. Any help? Here's my code.
Create a new Poll
<form id="myForm" action="/polls/" method="post">
{% csrf_token %}
Question: <input type="text" name="poll"><br />
<div id="Choices">
Choice: <input type="text" name="choice1"><br />
Choice: <input type="text" name="choice2"><br />
Choice: <input type="text" name="choice3"><br />
Choice: <input type="text" name="choice4"><br />
Choice: <input type="text" name="choice5"><br />
</div>
<a href="javascript:addOption();">Add option</a> <br />
<input type="submit" value="Submit" />
</form>
<script>
var choiceNumber = 6; //The first option to be added is number 6
function addOption() {
var choices = document.getElementById("Choices");
var newChoice = document.createElement("input");
newChoice.name = "choice"+choiceNumber;
newChoice.type = "text";
choices.appendChild(newChoice);
choiceNumber++;
}
</script>