?
Solved

Urgent Need help

Posted on 2010-01-10
3
Medium Priority
?
164 Views
Last Modified: 2012-05-08
Hello

I need to add  rows dynamically using javascript. My current code copies entrire first row. it is creating problems. I need java script to generate the code like this way. how my current javascript can be modifled. please advice me


current javascript
--------------------------

 function addRow(tableID) {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
            var colCount = table.rows[0].cells.length;
            for(var i=0; i<colCount; i++) {

                var newcell    = row.insertCell(i);

                newcell.innerHTML = table.rows[0].cells[i].innerHTML;

                //alert(newcell.childNodes);
                switch(newcell.childNodes[0].type) {

                    case "checkbox":
                            newcell.childNodes[0].checked = false;
                            break;
                    case "select-one":
                            newcell.childNodes[0].selectedIndex = 0;
                            break;
                }
            }
        }


generated code :
--------------------------

 <input type="button" value="Add  Associated Cases"
                 onclick="addRow('dataTable')" id="addCases"/>

     
        <input type="button" value="Delete Associated Cases "
               onclick="deleteRow('dataTable')"/>
     
        <br/>
        <br/>
        <table id="dataTable" width="350px" border="0">
     

  <tr>
     
      <TD><input type="text" name="cases[0].agencyCaseNumber" value=""></TD>

      <TD><input type="text" name="cases[0].caseStatusId" value=""></TD>
  </tr>

 <tr>
     
      <TD><input type="text" name="cases[0].agencyCaseNumber" value=""></TD>

      <TD><input type="text" name="cases[0].caseStatusId" value=""></TD>
  </tr>

         
        </table>

required code to be generated
------------------------------------------

<input type="button" value="Add  Associated Cases"
                 onclick="addRow('dataTable')" id="addCases"/>

     
        <input type="button" value="Delete Associated Cases "
               onclick="deleteRow('dataTable')"/>
     
        <br/>
        <br/>
        <table id="dataTable" width="350px" border="0">
     

  <tr>
     
      <TD><input type="text" name="cases[0].agencyCaseNumber" value=""></TD>

      <TD><input type="text" name="cases[0].caseStatusId" value=""></TD>
  </tr>

 <tr>
     
      <TD><input type="text" name="cases[1].agencyCaseNumber" value=""></TD>

      <TD><input type="text" name="cases[1].caseStatusId" value=""></TD>
  </tr>

         
        </table>


thanks





0
Comment
Question by:newbiejava
  • 2
3 Comments
 
LVL 13

Expert Comment

by:numberkruncher
ID: 26279097
The PrototypeJS framework would probably make things much simpler here (and probably more reliable due to severe memory related problems in IE that occur when dynamically adding or removing content).

I think that the following should achieve what you need. Please note that "prototype.js" is an open-source file and the associated website is: http://prototypejs.org/

Download PrototypeJS script from: http://prototypejs.org/assets/2009/8/31/prototype.js

I strongly recommend that you take a further look into what PrototypeJS can do for you, because it makes so many things much simpler, AND cross-browser friendly!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>Demo Page</title>
		<script type="text/javascript" src="prototype.js"></script>
		<script type="text/javascript">
			function addRow(tableId) {
				var body = $(tableId).select('tbody')[0];
				var newId = body.childElements().length;
				
				var row = new Element('tr'), agencyCaseNumber, caseStatusId;
				body.appendChild(row);

				row.appendChild(agencyCaseNumber = new Element('td'));
				agencyCaseNumber.appendChild(new Element("input", {
						type: "text",
						name: "cases[" + newId + "].agencyCaseNumber",
						value: ""
					}));

				row.appendChild(caseStatusId = new Element('td'));
				caseStatusId.appendChild(new Element("input", {
						type: "text",
						name: "cases[" + newId + "].caseStatusId",
						value: ""
					}));
			}
		</script>
	</head>
	<body>
		<h1>Demo Page</h1>
		<div>
			<input type="button" value="Add Associated Cases" onclick="addRow('dataTable');" id="addCases"/>
			<br/>
			<br/>
			<table id="dataTable" width="350px" border="0">
				<tr>
					<td>
						<input type="text" name="cases[0].agencyCaseNumber" value=""/>
					</td>
					<td>
						<input type="text" name="cases[0].caseStatusId" value=""/>
					</td>
				</tr>
			</table>
		</div>
	</body>
</html>

Open in new window

0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 26279343
Hello newbiejava,

Have a look to this code :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript">
	var n = 0;
	function addRow(tableID) {
		var table = document.getElementById(tableID);

		var tr = document.createElement('TR');

		var td1 = document.createElement('TD');
		var td2 = document.createElement('TD');

		var input1 = document.createElement('INPUT');
		var input2 = document.createElement('INPUT');
		
		input1.type = "text";
		input1.name = "cases[" + n + "].agencyCaseNumber";
		input1.setAttribute('id',"cases[" + n + "].agencyCaseNumber");
		
		input2.type = "text";
		input2.name = "cases[" + n + "].caseStatusId";
		input2.setAttribute('id',"cases[" + n + "].caseStatusId");
		
		td1.appendChild(input1);
		td2.appendChild(input2);
		tr.appendChild(td1);
		tr.appendChild(td2);
		table.appendChild(tr);
		
		n++;
	}
	
	function deleteRow(tableID) {
		var table = document.getElementById(tableID);
		var a = table.getElementsByTagName('TR');
		table.removeChild(a[a.length-1]);
		n--;
	}
</script>
</head>
<body>
    <input type="button" value="Add  Associated Cases" onclick="addRow('dataTable')" id="addCases"/>
    <input type="button" value="Delete Associated Cases" onclick="deleteRow('dataTable')"/>
    <br/>
    <br/>
    <table id="dataTable" width="350px" border="0"></table>
</body>
</html>

Open in new window

0
 
LVL 83

Expert Comment

by:leakim971
ID: 26296772
Thanks for the points! Happy new year!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

593 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