I have a form that begins with one select inside a div element. If the user wants to add another year, they click 'Add Another Year' which copies the div element below and clones it.
<div class="inputfield" id="modelyeardiv" class="modelyeardiv">
<select name="frmyears[]" tabindex="3">
<option value="0000">-- SELECT A YEAR --</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
</select>
<a href="#" class="addmodelyear">Add Another Year</a>
</div>
Clicking 'Add Another Year' allows me to clone it so that the user can select multiple years.
$(document.body).on('click', '.addmodelyear', function(e) {
e.preventDefault();
$('#modelyeardiv').after($('#modelyeardiv').clone());
return false;
});
Now when my form submits, I want to check if there is only one frmyears[] present. And if there is, I want to make sure the option value select is not 0000. This will force the user to select at least one year.
How should I go about this? I thought about creating a tracker within the jquery code above. So I'd increment it by 1 each time 'Add Another Year' is clicked. Upon submit, my jquery code would check this tracker. If it's 1, then see if the value is 0000 and prompt the user if so.
Is the efficient? Is there another way to accomplish this?
As a side note -- the client wants multiple drop downs like this. He does not want a multiple="multiple" list.