Reputation: 31
I tried writting a function which receives an input from the user as to how many field to create in a form. Then the function creates a Form which contains number(equivalent to user input) of textboxes and one submit button. I wrote the function but it doesnt seem to be working where have i gone wrong. I would be grateful if any one can help me. My code:
function createTextBox()
{
var box=document.getElementById('pop'); //getting the ID of the containner
var val=document.getElementById('uValue').value; //getting the Input value from the user
var candidateForm=document.createElement('form'); //Creating a form and giving the attributes
candidateForm.name="candidateForm";
candidateForm.method="post";
candidateFomr.action="process.php";
for(var i=0; i<val;i++)
{
var newTextbox = document.createElement('input'); //creating the textboxes according to the users input
newTextbox.type="textbox";
newTextbox.name="candidate[]";
newTextbox.id="candidateId";
candidateForm.appendChild(newTextbox); //putting the created textboxes in the form
}
var saveButton=document.createElement('input'); //creating the submit button which when clicked will save the value written in the textboxes
saveButton.type="submit";
saveButton.name="submitButton";
saveButton.value="Enter";
candidateForm.appendChild(saveButton); //putting the submit button in the form
box.appendChild(candiateForm); //And putting the form in the containner.
//alert (val);
}
here is the HTML part
<body>
<input type="textbox" name="value_box" id="uValue" ></input>
<input type="button" onclick="javascript:createTextBox()" value="Click"></input>
<div id="pop"></div>
</body>
Thanks in Advance :D
Upvotes: 0
Views: 5127
Reputation: 8348
You have two typos in your code. This:
candidateFomr.action="process.php";
Should be this:
candidateForm.action="process.php";
box.appendChild(candiateForm);
Should be this:
box.appendChild(candidateForm);
br
element after each one:
for(var i=0; i<val;i++)
{
var newTextbox = document.createElement('input'); //creating the textboxes according to the users input
newTextbox.type="textbox";
newTextbox.name="candidate[]";
newTextbox.id="candidateId";
candidateForm.appendChild(newTextbox); //putting the created textboxes in the form
var brElement = document.createElement("br");
candidateForm.appendChild(brElement);
}
for(var i=0; i<val;i++)
{
var lblElement = document.createElement("label");
lblElement.innerHTML = "Label " + (i+1) + " ";
candidateForm.appendChild(lblElement);
var newTextbox = document.createElement('input'); //creating the textboxes according to the users input
newTextbox.type="textbox";
newTextbox.name="candidate[]";
newTextbox.id="candidateId";
candidateForm.appendChild(newTextbox); //putting the created textboxes in the form
var brElement = document.createElement("br");
candidateForm.appendChild(brElement);
}
Upvotes: 0
Reputation: 9174
To begin with input
types are standalone tag.
Change your HTML to
<input type="textbox" name="value_box" id="uValue" />
<input type="button" onclick="javascript:createTextBox()" value="Click"/>
<div id="pop"></div>
Also made changes to the js
function createTextBox()
{
var box=document.getElementById('pop'); //getting the ID of the containner
var val=document.getElementById('uValue').value; //getting the Input value from the user
var candidateForm=document.createElement('form'); //Creating a form and giving the attributes
candidateForm.name="candidateForm";
candidateForm.method="post";
candidateFomr.action="process.php";
for(var i=0; i<val;i++)
{
var newTextbox = document.createElement('input'); //creating the textboxes according to the users input
newTextbox.type="textbox";
newTextbox.name="candidate[]";
newTextbox.id="candidateId";
candidateForm.appendChild(newTextbox); //putting the created textboxes in the form
}
var saveButton=document.createElement('input'); //creating the submit button which when clicked will save the value written in the textboxes
saveButton.type="submit";
saveButton.name="submitButton";
saveButton.value="Enter";
candidateForm.appendChild(saveButton); //putting the submit button in the form
box.appendChild(candiateForm); //And putting the form in the containner.
//alert (val);
}
Upvotes: 2
Reputation: 1987
An alternate method for you:
<script>
function generateForm()
{
$FormHTML = "";
$fieldCount = document.getElementById("fieldsCount").value;
console.log($fieldCount);
for ($i = 1; $i <= $fieldCount; $i++)
{
$FormHTML += "<div><b>Form input " + $i + ":</b><br><input type='text' id='element" + $i + "' name='element" + $i + "' style='width:200px;'/></div>\n";
}
document.getElementById("FieldsContainer").innerHTML = $FormHTML;
}
</script>
<form>
<select id="fieldsCount" name="fieldsCount" onChange="generateForm()">
<option value="0">Please choose</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="5">5</option>
<option value="10">10</option>
</select>
<div id="FieldsContainer"></div>
</form>
Upvotes: 1