Solved

Limit the number of rows created by java script

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can i retreive details from magento database 3 94
expanding and collapsing jquery 1 195
Jquery animation problem 7 58
Problem with syntax 17 123
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
Giving access to ESXi shell console is always an issue for IT departments to other Teams, or Projects. We need to find a way so that teams can use ESXTOP for their POCs, or tests without giving them the access to ESXi host shell console with a root …
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…

856 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