Avatar of capturetheflag
capturetheflag asked on

Limit the number of rows to 10 of my 'Add Row' function

I need to limit the number of rows I add to 10 in this function.
Thanks!
						$(document).on('click','#addAddOnDay',function(){
							var rowCount = $('#addOnDay tr').length + 1;
							var rowClass = (rowCount % 2 != 0) ? 'rowOdd' : 'rowEven';
							$('#addOnDay').append('<tr class="'+ rowClass +'"> \
																						<td align="center"><input type="text" class="text" style="width: 99%;" /></td> \
																						<td align="center"><input type="radio" name="4a_1"> <label>Yes</label> &nbsp; <input type="radio" name="4a_1" checked> <label>No</label></td> \
																						<td align="center">20000 or 95%</td> \
																						<td align="center" class="rollUpDisplay">1</td> \
																					</tr>');
						});

Open in new window

JavaScriptjQuery

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chris Stanyon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Rob

(no points) just putting it together for you:

$(document).on('click','#addAddOnDay',function(){
	var rowCount = $('#addOnDay tr').length + 1;
	if (rowCount > 10) {
		alert("You can't add any more rows!");
	} else {
		var rowClass = (rowCount % 2 != 0) ? 'rowOdd' : 'rowEven';
		$('#addOnDay').append('<tr class="'+ rowClass +'"> \
			<td align="center"><input type="text" class="text" style="width: 99%;" /></td> \
			<td align="center"><input type="radio" name="4a_1"> <label>Yes</label> &nbsp; <input type="radio" name="4a_1" checked> <label>No</label></td> \
			<td align="center">20000 or 95%</td> \
			<td align="center" class="rollUpDisplay">1</td> \
			</tr> \
		');
	};
});

Open in new window

ASKER
capturetheflag

Hello Chris and tagit,

Thanks for the help.  I have to change rowCount > 10, to rowCount > 11 in order to get 10 rows. rowCount > 10 just gave me 9 rows.  Is there a reason for this? rowCount >= 10 gave me just 9 rows too.
Chris Stanyon

Not sure really. It should give you ten rows. If you use >= 10, then you'll only get 9, but using > 10 should give you 10. Maybe it's because you add 1 to the rowCount for some reason. Here's a fiddle that shows it working:

http://jsfiddle.net/ChrisStanyon/Kj4yH/

I can take more of a look if you post your code (including the HTML) or preferably give us a link to a page that demonstrates the problem.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
capturetheflag

Thanks for the help
Chris Stanyon

Agree with @tagit - the answer posted was pretty much a copy and paste answer so can't understand why it's only a B