Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 858
  • Last Modified:

Inserting dynamic rows between dynamic rows using javascript

Hi i jus need to add dynamic rows between the rows which are created onclick of "addLocation" button. The concept is sites can have many location, locations can have many products and products can have many fees. please let me if u have any doubts !. Do reply soon
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Dynamic Table</title>
<script language="javascript">
function addSite()
{
 
	alert("ADD SITE ");
	var myTable = document.getElementById("myTable");
	var newTR = document.createElement("tr");
	var newName1 = document.createElement("td");
	var newName2 = document.createElement("td");
	var newName3 = document.createElement("td");
	var newName4 = document.createElement("td");
	var newName5 = document.createElement("td");
	var newName6 = document.createElement("td");
	var newName7 = document.createElement("td");
	var newName8 = document.createElement("td");
	var newName9 = document.createElement("td");
	var newName10 = document.createElement("td");
	var newName11 = document.createElement("td");
	
	newName1.innerHTML="<input type='text' name='dynamicSite'>";
	newName2.innerHTML="<a href='resumepage.html'>Site Details</a>";
	newName3.innerHTML="<input type='text' name='dynamicLocation'>";
	newName4.innerHTML="<input type='button' name='dynamicLocationPlus' value=' addLocation ' onclick='addLocation(this.parentNode.parentNode.rowIndex)'>";
	newName5.innerHTML="<input type='text' name='dynamicProduct'>";
	newName6.innerHTML="<input type='button' name='dynamicProductLookup' value=' ^ '>";
	newName7.innerHTML="<input type='button' name='dynamicProductPlus' value=' addProduct ' onclick='addProduct(this.parentNode.parentNode.rowIndex)'>";
	newName8.innerHTML="<select name='dynamicFee'><option selected='selected'>Setup Fee</option><option>Training Fee</option><option>Misc Fee</option></select>";
	newName9.innerHTML="<input type='button' name='dynamicFeePlus' value=' addFee ' onclick='addFee(this.parentNode.parentNode.rowIndex)'>";
	newName10.innerHTML="<input type='button' name='dynamicFeeSetup' value=' FeeSetup '>";
	newName11.innerHTML="<input type='button'  value=' deleteRow ' onclick='deleteRow(this.parentNode.parentNode.rowIndex)'/>";
 
	newTR.appendChild(newName1);
	newTR.appendChild(newName2);
	newTR.appendChild(newName3);
	newTR.appendChild(newName4);
	newTR.appendChild(newName5);
	newTR.appendChild(newName6);
	newTR.appendChild(newName7);
	newTR.appendChild(newName8);
	newTR.appendChild(newName9);
	newTR.appendChild(newName10);
	newTR.appendChild(newName11);
	myTable.appendChild(newTR);
}
 
function addLocation(i){
 
        alert("ADD LOCATION " + i);
        var myTable = document.getElementById("myTable");
        var newTR = document.createElement("tr");
        var newName1 = document.createElement("td");
        var newName2 = document.createElement("td");
        var newName3 = document.createElement("td");
        var newName4 = document.createElement("td");
        var newName5 = document.createElement("td");
        var newName6 = document.createElement("td");
        var newName7 = document.createElement("td");
        var newName8 = document.createElement("td");
		var newName9 = document.createElement("td");
		var newName10 = document.createElement("td");
		var newName11 = document.createElement("td");
 
		newName1.innerHTML="<input type='hidden'>"
		newName2.innerHTML="<input type='hidden'>"
        newName3.innerHTML="<input type='text' name='dynamicLocationLocation'>";
		newName4.innerHTML="<input type='hidden'>"
        newName5.innerHTML="<input type='text' name='dynamicLocationProduct'>";
        newName6.innerHTML="<input type='button' name=' ' value=' ^ '>";
        newName7.innerHTML="<input type='button' name='dynamicLocationProductPlus' value=' addProduct ' onclick='addProduct(this.parentNode.parentNode.rowIndex)' >";
        newName8.innerHTML="<select name='dynamicLocationFee'><option selected='selected'>Setup Fee</option><option>Training Fee</option><option>Misc Fee</option></select>";
        newName9.innerHTML="<input type='button' name='dynamicLocationFeePlus' value=' addFee ' onclick='addFee(this.parentNode.parentNode.rowIndex)'>";
        newName10.innerHTML="<input type='button' name='dynamicLocationFeeSetup' value=' FeeSetup '>";
        newName11.innerHTML="<input type='button'  value=' deleteRow ' onclick='deleteRow(this.parentNode.parentNode.rowIndex)'/>";
      
        newTR.appendChild(newName1);
        newTR.appendChild(newName2);
        newTR.appendChild(newName3);
        newTR.appendChild(newName4);
        newTR.appendChild(newName5);
        newTR.appendChild(newName6);
        newTR.appendChild(newName7);
        newTR.appendChild(newName8);
		newTR.appendChild(newName9);
        newTR.appendChild(newName10);
		newTR.appendChild(newName11);
        myTable.rows(i).appendChild(newTR);
}
 
 function addProduct(i){
 
        alert("ADD PRODUCT " + i);
        var myTable = document.getElementById("myTable");
 
		var newTR = document.createElement("tr");
        var newName1 = document.createElement("td");
        var newName2 = document.createElement("td");
        var newName3 = document.createElement("td");
        var newName4 = document.createElement("td");
        var newName5 = document.createElement("td");
        var newName6 = document.createElement("td");
		var newName7 = document.createElement("td");
		var newName8 = document.createElement("td");
		var newName9 = document.createElement("td");
		var newName10 = document.createElement("td");
		var newName11 = document.createElement("td");
 
        newName1.innerHTML="<input type='hidden'>"
		newName2.innerHTML="<input type='hidden'>"
		newName3.innerHTML="<input type='hidden'>"
		newName4.innerHTML="<input type='hidden'>"
		newName5.innerHTML="<input type='hidden'>"
        newName6.innerHTML="<input type='text' name='dynamicLocationProduct'>";
        newName7.innerHTML="<input type='button' name=' ' value=' ^ '>";
        newName8.innerHTML="<select name='dynamicLocationFee'><option selected='selected'>Setup Fee</option><option>Training Fee</option><option>Misc Fee</option></select>";
        newName9.innerHTML="<input type='button' name='dynamicProductFeePlus' value=' addFee ' onclick='addFee(this.parentNode.parentNode.rowIndex)'>";
        newName10.innerHTML="<input type='button' name='dynamicProductFeeSetup' value=' FeeSetup '>";
        newName11.innerHTML="<input type='button'  value=' deleteRow ' onclick='deleteRow(this.parentNode.parentNode.rowIndex)'/>";
 
        newTR.appendChild(newName1);
        newTR.appendChild(newName2);
        newTR.appendChild(newName3);
        newTR.appendChild(newName4);
        newTR.appendChild(newName5);
        newTR.appendChild(newName6);
		newTR.appendChild(newName7);
        newTR.appendChild(newName8);
        newTR.appendChild(newName9);
        newTR.appendChild(newName10);
        newTR.appendChild(newName11);
        myTable.rows(i).appendChild(newTR);
}
 
 function addFee(i){
 
        alert("ADD FEE " + i);
        var myTable = document.getElementById("myTable");
        var newTR = document.createElement("tr");
        var newName1 = document.createElement("td");
        var newName2 = document.createElement("td");
        var newName3 = document.createElement("td");
		var newName4 = document.createElement("td");
        var newName5 = document.createElement("td");
        var newName6 = document.createElement("td");
		var newName7 = document.createElement("td");
		var newName8 = document.createElement("td");
		var newName9 = document.createElement("td");
		var newName10 = document.createElement("td");
		var newName11 = document.createElement("td");
 
 		newName1.innerHTML="<input type='hidden'>"
		newName2.innerHTML="<input type='hidden'>"
		newName3.innerHTML="<input type='hidden'>"
        newName4.innerHTML="<input type='hidden'>"
		newName5.innerHTML="<input type='hidden'>"
		newName6.innerHTML="<input type='hidden'>"
		newName7.innerHTML="<input type='hidden'>"
		newName8.innerHTML="<input type='hidden'>"
        newName9.innerHTML="<select name='dynamicLocationFee'><option selected='selected'>Setup Fee</option><option>Training Fee</option><option>Misc Fee</option></select>";
        newName10.innerHTML="<input type='button' name='dynamicLocationFeeSetup' value=' FeeSetup '>";
        newName11.innerHTML="<input type='button'  value=' deleteRow ' onclick='deleteRow(this.parentNode.parentNode.rowIndex)'/>";
 
        newTR.appendChild(newName1);
        newTR.appendChild(newName2);
        newTR.appendChild(newName3);
		newTR.appendChild(newName4);
        newTR.appendChild(newName5);
        newTR.appendChild(newName6);
		newTR.appendChild(newName7);
        newTR.appendChild(newName8);
        newTR.appendChild(newName9);
        newTR.appendChild(newName10);
        newTR.appendChild(newName11);
        myTable.rows(i).appendChild(newTR);
}
 
function deleteRow(i){
	alert(i);
   document.getElementById('myTable').deleteRow(i);
}
 
function validate(){
	
	alert("Submit");
}
</script>
</head>
<body>
 
<form name="form">
<table align="right">
	<tr>
		<td><input type="button" value=" addRow " onClick="addSite()"></td>
	</tr>
</table>
<br>
<table>
<tbody id='myTable'>
	<tr>
		<td><input type="text" name=site></td>
		<td><a href ="javascript:void(0)" onclick="window.open('login.html')">Site Details</a></td>
		<td><input type="text" name=location></td>
		<td><input type="button" name="locationPlus" value=" addLocation " onclick='addLocation(this.parentNode.parentNode.rowIndex)'></td>
		<td><input type="text" name=product></td>
		<td><input type="button" name="productLookup" value=" ^ "></td>
		<td><input type="button" name="productPlus" value=" addProduct " onclick='addProduct(this.parentNode.parentNode.rowIndex)'></td>
		<td><select name="fee" id="fee">
                <option selected="selected">Setup fee</option>
                <option>Training Fee</option>
                <option>Misc Fee</option>
			 </select>
		</td>
			
		<td><input type="button" name="feePlus" value=" addFee " onclick='addFee(this.parentNode.parentNode.rowIndex)'></td>
		<td><input type='button' name="feeSetup" value=" feeSetup " onclick='addFee(this.parentNode.parentNode.rowIndex)'></td>
		<td><input type="button"  value=" deleteRow " onclick='deleteRow(this.parentNode.parentNode.rowIndex)'/></td>
	</tr>
 
</tbody>
</table>
<br>
<table align="center">
	<tr>
		<td>
			<input type="submit" value=" Submit " onclick="validate()">
		</td>
	</tr>
</table>
</form>
</body>
</html>

Open in new window

0
Isotropes
Asked:
Isotropes
1 Solution
 
alexpercsiCommented:
I'm pretty sure  myTable.rows(i).appendChild(newTR); won't work. I think you would be appending a table row to a table row.

Here's one alternative:

var whereToAdd=56;
var newTable = document.createElement("table")
var oldTable=document.createElement("oldTable");
var tableRows=oldTable.getElementsByTagName("tr");
for (i=0;i<whereToAdd;i++)
{
    newTable.appendChild(tableRows[i]);
}
newTable.appendChild(newTR);
for (i=whereToAdd;i<tableRows.length;i++)
{
    newTable.appendChild(tableRows[i]);
}

//delete old table
//insert new table in the place of the old table


I hope this helps.
Best Regards,
Alex Percsi.
0
 
IsotropesAuthor Commented:
Got Cleared :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now