I'm putting together a simple form validation, but I seem to be having an issue with checking for null value in a few of my select elements. I've abbreviated the option lists in my example below...
<select class='species_select' id='species'+siteNumber+'1'>
<option disabled selected>Select species</option>
<option value='unknown'>unknown</option>
</select>
<select class='species_select' id='species'+siteNumber+'2'>
<option disabled selected>Select species</option>
<option value='unknown'>unknown</option>
</select>
<script>
$(document).on("click", "#next_to_summary", function(){
var proceed = true;
$(".species_select").each(function() {
if (this.value === null){
alert("Please select the species");
proceed = false;
};
});
</script>
While experimenting with this, I found that if I assigned a value to the disabled selected
option and checked for that, the code worked just fine - the alert fired when the select had not been changed. I'd rather check for null, though...
Am I not checking for null correctly? Thanks very much.