Link to home
Start Free TrialLog in
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

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(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

Avatar of capturetheflag
capturetheflag

ASKER

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.
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.
Thanks for the help
Agree with @tagit - the answer posted was pretty much a copy and paste answer so can't understand why it's only a B