Solved

Limit the number of rows created by java script

Posted on 2011-03-01
1
475 Views
Last Modified: 2012-05-11
I found this nice little Java script to add table rows on click... I’m not familiar with java and I need to limit the number row added, say only 10 rows can be created. Can someone help me on this? Thanks
<script type="text/JavaScript">
function addRow(r){
var root = r.parentNode;//the root
var allRows = root.getElementsByTagName('tr');//the rows' collection
var cRow = allRows[0].cloneNode(true)//the clone of the 1st row
var cInp = cRow.getElementsByTagName('input');//the inputs' collection of the 1st row

for(var i=0;i<cInp.length;i++){//changes the inputs' names (indexes the names)
cInp[i].setAttribute('name',cInp[i].getAttribute('name')+'_'+(allRows.length+1))
}
root.appendChild(cRow);//appends the cloned row as a new row
}
</script>

Open in new window

0
Comment
Question by:inrworx
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 35012955
Use :


<script type="text/JavaScript">
	function addRow(r){
		var root = r.parentNode;//the root
		var allRows = root.getElementsByTagName('tr');//the rows' collection
		if(allRows.length<10) {
			var cRow = allRows[0].cloneNode(true)//the clone of the 1st row
			var cInp = cRow.getElementsByTagName('input');//the inputs' collection of the 1st row
			
			for(var i=0;i<cInp.length;i++){//changes the inputs' names (indexes the names)
				cInp[i].setAttribute('name',cInp[i].getAttribute('name')+'_'+(allRows.length+1))
			}
			root.appendChild(cRow);//appends the cloned row as a new row
		}
		else {
			alert("You can' add a new row");
		}
	}
</script>

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery Popover 4 376
Resources for learning AutoLISP 1 66
GridMvc Error mvc5 5 215
Export data and graph to excel 11 490
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
No single Antivirus application (despite claims by manufacturers) will catch or protect you from all Virus / Malware or Spyware threats. That doesn't stop you from further protecting yourself however - and this article is to show you how.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

713 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