Solved

Limit the number of rows created by java script

Posted on 2011-03-01
1
469 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In  today’s increasingly digital world, managed service providers (MSPs) fight for their customers’ attention, looking for ways to make them stay and purchase more services. One way to encourage that behavior is to develop a dependable brand of prod…
Find out what the Office 365 disclaimer function is, why you would use it and its limited ability to create Office 365 signatures.
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now