Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Inserting dynamic rows between dynamic rows using javascript

Posted on 2008-09-30
2
Medium Priority
?
852 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

715 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