Hi all, would like to convert this javaScript code versatile so it will be able to extend various inputs of a form.
var instance = 0;
function moreFields() {
// Check if there isn't more than 3 fields
if(instance != 3) {
instance++;
// Create a child
var clone = document.getElementById('water_src').cloneNode(true);
// Make child unique
clone.id += instance;
clone.name += instance;
clone.style.display = 'block';
// Locate clone's home
var insertHere = document.getElementById('water_drop');
// Place clone in home
insertHere.parentNode.insertBefore(clone,insertHere);
}
}
window.onload = moreFields;
I do this by adding the parentHome & cloneHome parameters to the function and changing the 'water_src' & 'water_drop' to the corresponding ones.
var instance = 0;
function moreFields(parentHome, cloneHome) {
// Check if there isn't more than 3 fields
if(instance != 3) {
instance++;
// Create a child
var clone = document.getElementById(parentHome).cloneNode(true);
// Make child unique
clone.id += instance;
clone.name += instance;
clone.style.display = 'block';
// Locate clone's home
var insertHere = document.getElementById(cloneHome);
// Place clone in home
insertHere.parentNode.insertBefore(clone,insertHere);
}
}
window.onload = moreFields;
Unfortunetaly when I do this change not even the parent field appears. HTML code bellow
<div id="water_src" name="water_src" style="display: none">
<select>
<option></option>
<?php
while($row=mysql_fetch_array($result)) {
echo "\n\t\t\t\t<option value=".$row['water_ID'].">".$row['water_name']."</option>";
}
echo "\n";
?>
</select>
<input type="button" value="-"
onclick="if(instance > 1) { instance--; this.parentNode.parentNode.removeChild(this.parentNode);}" />
<input type="button" value="+"
onclick="moreFields('water_src', 'water_drop');" />
<br />
</div>
<form method="post" action="package_builder.php">
<span id="water_drop"></span>
<input type="submit" value="Send form" />
</form>
Is there any errors with my syntax or logic?