Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Inserting dynamic rows between dynamic rows using javascript

Posted on 2008-09-30
2
Medium Priority
?
854 Views
Last Modified: 2012-06-27
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
Comment
Question by:Isotropes
2 Comments
 
LVL 7

Accepted Solution

by:
alexpercsi earned 750 total points
ID: 22669632
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
 

Author Comment

by:Isotropes
ID: 22676876
Got Cleared :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question