html
<select name="register-month" id="register-month">
<option value="00">Month</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
jquery
function checkbirthday() {
var register_month_value = $("#register-month").val();
//month check
if (register_month_value === "") {
$("#register-month option[value='00']").remove();
$('#register-month').css('border-color', 'red');
} else {
$('#register-month').css('border-color', '#dfe0e6');
}
return;
}
$(document).ready(function() {
$("#register-month").keyup(checkbirthday);
});
My intention was simple, 1st I wanted to disable the "Month" once they click on the selection, 2nd if the selection is empty value, change the css border to red.
But I tried many ways, still no luck, what is wrong here?
See comments inline:
// on change of the option selection
$('#register-month').on('change', function() {
var selectedMonth = parseInt($(this).val(), 10); // Get value of selected option
var borderColor;
if (selectedMonth) { // If selected option is `Month`
$(this).find('option[value="00"]').prop('disabled', true);
borderColor = '#dfe0e6';
} else {
borderColor = 'red';
}
$(this).css('border-color', borderColor); // Update the border colour of the dropdown.
});
Demo: http://jsfiddle.net/tusharj/7w5ub8t9/
Simply change to
if(register_month_value === "00")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With