I'm working with a SharePoint 2013 NewForm. The list it relates to contains fields to hold information about batch files that being submitted, 4 fields per file. (So if 2 files are being submitted, then I need to display 4 fields per file => 8 fields)
I start by getting the number of files that are being submitted. Then I display all the rows that are needed to for that number of files.
I'm a beginner at this. The code below works but it's so clunky. What is a better way of doing it?
//display the correct number of batch info rows
$("input[title='How many batches have you split the job into? Required Field']").change(function(){
var numberPages = $('input[title="How many batches have you split the job into? Required Field"]').val();
//convert the string to a number
var n = Math.floor(Number(numberPages));
//check to see if the number is NaN (not a number)
if (isNaN(n)) {
alert("Please enter a number");
//If it is a number, display the correct number of rows, and hide the ones that are not needed (user may change their input to a lower number)
} else if (n == 1) {
$('nobr:contains("Batch 1 File name")').closest('tr').show();
$('nobr:contains("Batch 1 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 1 First name and address")').closest('tr').show();
$('nobr:contains("Batch 1 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 2 File name")').closest('tr').hide();
$('nobr:contains("Batch 2 - number of letters in batch")').closest('tr').hide();
$('nobr:contains("Batch 2 First name and address")').closest('tr').hide();
$('nobr:contains("Batch 2 Last name and address")').closest('tr').hide();
$('nobr:contains("Batch 3 File name")').closest('tr').hide();
$('nobr:contains("Batch 3 - number of letters in batch")').closest('tr').hide();
$('nobr:contains("Batch 3 First name and address")').closest('tr').hide();
$('nobr:contains("Batch 3 Last name and address")').closest('tr').hide();
$('nobr:contains("Batch 4 File name")').closest('tr').hide();
$('nobr:contains("Batch 4 - number of letters in batch")').closest('tr').hide();
$('nobr:contains("Batch 4 First name and address")').closest('tr').hide();
$('nobr:contains("Batch 4 Last name and address")').closest('tr').hide();
$('nobr:contains("Batch 5 File name")').closest('tr').hide();
$('nobr:contains("Batch 5 - number of letters in batch")').closest('tr').hide();
$('nobr:contains("Batch 5 First name and address")').closest('tr').hide();
$('nobr:contains("Batch 5 Last name and address")').closest('tr').hide();
$('nobr:contains("Batch 6 File name")').closest('tr').hide();
$('nobr:contains("Batch 6 - number of letters in batch")').closest('tr').hide();
$('nobr:contains("Batch 6 First name and address")').closest('tr').hide();
$('nobr:contains("Batch 6 Last name and address")').closest('tr').hide();
} else if (n == 2) {
$('nobr:contains("Batch 1 File name")').closest('tr').show();
$('nobr:contains("Batch 1 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 1 First name and address")').closest('tr').show();
$('nobr:contains("Batch 1 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 2 File name")').closest('tr').show();
$('nobr:contains("Batch 2 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 2 First name and address")').closest('tr').show();
$('nobr:contains("Batch 2 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 3 File name")').closest('tr').hide();
$('nobr:contains("Batch 3 - number of letters in batch")').closest('tr').hide();
$('nobr:contains("Batch 3 First name and address")').closest('tr').hide();
$('nobr:contains("Batch 3 Last name and address")').closest('tr').hide();
$('nobr:contains("Batch 4 File name")').closest('tr').hide();
$('nobr:contains("Batch 4 - number of letters in batch")').closest('tr').hide();
$('nobr:contains("Batch 4 First name and address")').closest('tr').hide();
$('nobr:contains("Batch 4 Last name and address")').closest('tr').hide();
$('nobr:contains("Batch 5 File name")').closest('tr').hide();
$('nobr:contains("Batch 5 - number of letters in batch")').closest('tr').hide();
$('nobr:contains("Batch 5 First name and address")').closest('tr').hide();
$('nobr:contains("Batch 5 Last name and address")').closest('tr').hide();
$('nobr:contains("Batch 6 File name")').closest('tr').hide();
$('nobr:contains("Batch 6 - number of letters in batch")').closest('tr').hide();
$('nobr:contains("Batch 6 First name and address")').closest('tr').hide();
$('nobr:contains("Batch 6 Last name and address")').closest('tr').hide();
} else if (n == 3) {
$('nobr:contains("Batch 1 File name")').closest('tr').show();
$('nobr:contains("Batch 1 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 1 First name and address")').closest('tr').show();
$('nobr:contains("Batch 1 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 2 File name")').closest('tr').show();
$('nobr:contains("Batch 2 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 2 First name and address")').closest('tr').show();
$('nobr:contains("Batch 2 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 3 File name")').closest('tr').show();
$('nobr:contains("Batch 3 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 3 First name and address")').closest('tr').show();
$('nobr:contains("Batch 3 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 4 File name")').closest('tr').hide();
$('nobr:contains("Batch 4 - number of letters in batch")').closest('tr').hide();
$('nobr:contains("Batch 4 First name and address")').closest('tr').hide();
$('nobr:contains("Batch 4 Last name and address")').closest('tr').hide();
$('nobr:contains("Batch 5 File name")').closest('tr').hide();
$('nobr:contains("Batch 5 - number of letters in batch")').closest('tr').hide();
$('nobr:contains("Batch 5 First name and address")').closest('tr').hide();
$('nobr:contains("Batch 5 Last name and address")').closest('tr').hide();
$('nobr:contains("Batch 6 File name")').closest('tr').hide();
$('nobr:contains("Batch 6 - number of letters in batch")').closest('tr').hide();
$('nobr:contains("Batch 6 First name and address")').closest('tr').hide();
$('nobr:contains("Batch 6 Last name and address")').closest('tr').hide();
} else if (n == 4) {
$('nobr:contains("Batch 1 File name")').closest('tr').show();
$('nobr:contains("Batch 1 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 1 First name and address")').closest('tr').show();
$('nobr:contains("Batch 1 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 2 File name")').closest('tr').show();
$('nobr:contains("Batch 2 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 2 First name and address")').closest('tr').show();
$('nobr:contains("Batch 2 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 3 File name")').closest('tr').show();
$('nobr:contains("Batch 3 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 3 First name and address")').closest('tr').show();
$('nobr:contains("Batch 3 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 4 File name")').closest('tr').show();
$('nobr:contains("Batch 4 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 4 First name and address")').closest('tr').show();
$('nobr:contains("Batch 4 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 5 File name")').closest('tr').hide();
$('nobr:contains("Batch 5 - number of letters in batch")').closest('tr').hide();
$('nobr:contains("Batch 5 First name and address")').closest('tr').hide();
$('nobr:contains("Batch 5 Last name and address")').closest('tr').hide();
$('nobr:contains("Batch 6 File name")').closest('tr').hide();
$('nobr:contains("Batch 6 - number of letters in batch")').closest('tr').hide();
$('nobr:contains("Batch 6 First name and address")').closest('tr').hide();
$('nobr:contains("Batch 6 Last name and address")').closest('tr').hide();
} else if (n == 5) {
$('nobr:contains("Batch 1 File name")').closest('tr').show();
$('nobr:contains("Batch 1 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 1 First name and address")').closest('tr').show();
$('nobr:contains("Batch 1 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 2 File name")').closest('tr').show();
$('nobr:contains("Batch 2 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 2 First name and address")').closest('tr').show();
$('nobr:contains("Batch 2 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 3 File name")').closest('tr').show();
$('nobr:contains("Batch 3 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 3 First name and address")').closest('tr').show();
$('nobr:contains("Batch 3 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 4 File name")').closest('tr').show();
$('nobr:contains("Batch 4 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 4 First name and address")').closest('tr').show();
$('nobr:contains("Batch 4 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 5 File name")').closest('tr').show();
$('nobr:contains("Batch 5 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 5 First name and address")').closest('tr').show();
$('nobr:contains("Batch 5 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 6 File name")').closest('tr').hide();
$('nobr:contains("Batch 6 - number of letters in batch")').closest('tr').hide();
$('nobr:contains("Batch 6 First name and address")').closest('tr').hide();
$('nobr:contains("Batch 6 Last name and address")').closest('tr').hide();
} else if (n == 6) {
$('nobr:contains("Batch 1 File name")').closest('tr').show();
$('nobr:contains("Batch 1 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 1 First name and address")').closest('tr').show();
$('nobr:contains("Batch 1 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 2 File name")').closest('tr').show();
$('nobr:contains("Batch 2 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 2 First name and address")').closest('tr').show();
$('nobr:contains("Batch 2 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 3 File name")').closest('tr').show();
$('nobr:contains("Batch 3 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 3 First name and address")').closest('tr').show();
$('nobr:contains("Batch 3 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 4 File name")').closest('tr').show();
$('nobr:contains("Batch 4 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 4 First name and address")').closest('tr').show();
$('nobr:contains("Batch 4 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 5 File name")').closest('tr').show();
$('nobr:contains("Batch 5 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 5 First name and address")').closest('tr').show();
$('nobr:contains("Batch 5 Last name and address")').closest('tr').show();
$('nobr:contains("Batch 6 File name")').closest('tr').show();
$('nobr:contains("Batch 6 - number of letters in batch")').closest('tr').show();
$('nobr:contains("Batch 6 First name and address")').closest('tr').show();
$('nobr:contains("Batch 6 Last name and address")').closest('tr').show();
} else {
alert("If you need to do more than 6 batches, please submit more than one request.");
}
});