I'm inexperienced with Javascript and I found this script, but I'm not sure how to alter it to do what I want. I have a PHP script that creates forms in a loop. $z in this case could equal 3, and 3 forms each containing 1 Position/Office field will be created including 1 Candidate field. If a user wants to add a new candidate for that specific position, I want to be able to add a Candidate text field just relevant to that Office/Position. With this script, I've only been able to pop up a text field under one Office/Position and not under any other Office/Position. I think it has something to do with my divs. Any help will be greatly appreciated!
Javascript:
var inival=0; // Initialise starting element number
// Call this function to add textbox
function addTextBox()
{
var newArea = add_New_Element();
var htcontents = "Candidate: <input name='candidate' type='text' id='candidate'> echo 'divIdName';";
document.getElementById(newArea).inner… = htcontents; // You can any other elements in place of 'htcontents'
}
function add_New_Element() {
inival=inival+1; // Increment element number by 1
var ni = document.getElementById('area');
var newdiv = document.createElement('div'); // Create dynamic element
var divIdName = 'my'+inival+'Div';
newdiv.setAttribute('id',divIdName);
ni.appendChild(newdiv);
return divIdName;
}
--------------------------------------…
PHP Code:
<?
echo "<table>";
//***** For loop for the amount of positions chosen on previous page *****
for ($z = 1; $z <= $position_num; $z++){
echo "<tr>";
echo "<form method='post' action='uploadvoters.php'>";
echo "<p>Office/Position $z <span class = 'required'></span>:";
echo " ";
echo "<input name='position' type='text' id='position'>";
echo "</p>";
echo "<p>";
echo "Candidate : ";
echo " ";
echo "<input name='candidate' type='text' id='candidate'>";
echo "<br/><br/>";
echo "<div id='area'.$z>";
echo "</div>";
echo "<a onclick='addTextBox()' href='#'>Add Candidate </a>";
echo " Add Write In Candidate ";
echo "<input type='checkbox' name='writein' value='writein'><br>";
echo "</p>";
echo "<td></td><td></td><td></td>";
echo "</tr>";
echo "</form>";
echo "<hr></hr>";
} //***** END OF Z LOOP *****
echo "<tr><td>";
echo "</td><td>";
echo"</td><td>";
echo "</td></tr>";
echo "</table>";
?>