I am using an O/S plugin script that produces id's that look like array elements as follows:
<select name="serviceTypeID[1]" id="serviceTypeID[1]" ...
<select name="serviceTypeID[2]" id="serviceTypeID[2]" ...
<select name="serviceTypeID[3]" id="serviceTypeID[3]" ...
<select name="serviceTypeID[4]" id="serviceTypeID[4]" ...
<select name="serviceTypeID[5]" id="serviceTypeID[5]" ...
<select name="serviceTypeID[6]" id="serviceTypeID[6]" ...
<select name="serviceTypeID[7]" id="serviceTypeID[7]" ...
<select name="serviceTypeID[8]" id="serviceTypeID[8]" ...
<select name="serviceTypeID[9]" id="serviceTypeID[9]" ...
<select name="serviceTypeID[10]" id="serviceTypeID[10]" ...
How can I condense the following jquery script so I do not have to hard-code-repeat the block for all 10 instances of the html select element? Can I setup a hidden form element with the index value set to it and use something like this.().val() ? This is a bit beyond my coding skills. Thanks.
$('#serviceTypeID\\[1\\],#serviceTypeID\\[2\\],#serviceTypeID\\[3\\],#serviceTypeID\\[4\\],#serviceTypeID\\[5\\],#serviceTypeID\\[6\\],#serviceTypeID\\[7\\],#serviceTypeID\\[8\\],#serviceTypeID\\[9\\],#serviceTypeID\\[10\\]').change(function() {
// 1st identical instance of the block
var first = parseInt( $('#firstService\\[1\\]').val() );
var second = parseInt( $('#secondService\\[1\\]').val() );
var third = parseInt( $('#thirdService\\[1\\]').val() );
if (isNaN(first)) first = 0;
if (isNaN(second)) second = 0;
if (isNaN(third)) third = 0;
$('#serviceTotal\\[1\\]').val( ( first + second + third + ' Total') );
// 2nd identical instance of the block
first = parseInt( $('#firstService\\[2\\]').val() );
second = parseInt( $('#secondService\\[2\\]').val() );
third = parseInt( $('#thirdService\\[2\\]').val() );
if (isNaN(first)) first = 0;
if (isNaN(second)) second = 0;
if (isNaN(third)) third = 0;
$('#serviceTotal\\[2\\]').val( ( first + second + third + ' Total') );
// 3rd identical instance of the block
first = parseInt( $('#firstService\\[3\\]').val() );
second = parseInt( $('#secondService\\[3\\]').val() );
third = parseInt( $('#thirdService\\[3\\]').val() );
if (isNaN(first)) first = 0;
if (isNaN(second)) second = 0;
if (isNaN(third)) third = 0;
$('#serviceTotal\\[3\\]').val( ( first + second + third + ' Total') );
//and so on up to 10 currently
// 10th identical instance of the block
first = parseInt( $('#firstService\\[10\\]').val() );
second = parseInt( $('#secondService\\[10\\]').val() );
third = parseInt( $('#thirdService\\[10\\]').val() );
if (isNaN(first)) first = 0;
if (isNaN(second)) second = 0;
if (isNaN(third)) third = 0;
$('#serviceTotal\\[10\\]').val( ( first + second + third + ' Total') );
});