2

Add dynamically rows when user click on button. I made a script but it does not work plz help me out

   
<script>
var t;
t=2;
function Insert(){
var tab=document.getElementById("mytable");

var row=insertRow(t);
var cell1=insertCell(0);
var cell2=insertCell(1);
var cell3=insertCell(2);
var cell4=insertCell(3);
 
t=t+1;
 
 
   cell2.setAttribute('contenteditable','true');
   cell3.setAttribute('contenteditable','true');
   cell4.setAttribute('contenteditable','true');
}
</script>
Html 
<input type="button"  onclick="Insert()" name="Insert Row" value="Insert Row" />    

2 Answers 2

2

function addRow(tableID) {

			var table = document.getElementById(tableID);

			var rowCount = table.rows.length;
			var row = table.insertRow(rowCount);


			var cell3 = row.insertCell(0);
			var element2 = document.createElement("input");
			element2.type = "text";
			element2.name = "txtbox[]";
			cell3.appendChild(element2);


		}
<BODY>

	<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />


	<TABLE id="dataTable" width="100px" border="1">
		<TR>
			<TD> <INPUT type="text" /> </TD>
		</TR>
	</TABLE>

</BODY>

Sign up to request clarification or add additional context in comments.

2 Comments

done thank you so much please refer me some good book for java script
You can download pdf from tutorialspoint.com, you can learn from that site
1

change your script like

<script>
var t;
t=2;
function Insert(){
var tab=document.getElementById("mytable");

var row=tab.insertRow(t);
var cell1=row.insertCell(0);
var cell2=row.insertCell(1);
var cell3=row.insertCell(2);
var cell4=row.insertCell(3);
 
t=t+1;
 
 
   cell2.setAttribute('contenteditable','true');
   cell3.setAttribute('contenteditable','true');
   cell4.setAttribute('contenteditable','true');
}
</script>

3 Comments

insertRow and insertCell are Object methods, in your script you use them as functions. Object methods, as the name tells you; require an object in order to be called. This is done with the ' . ' which is a property accessor.
you get table id and store that in tab variable now you inserting the row without mention table so where it will enter the row
done thank you so much please refer me some good book for java script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.