The problem is that deleting items from the second list occurs in two places.
In the setDays function, all elements are deleted and the necessary ones are added for the selected month.
In the handler function of the change event - the first elements in both lists are deleted during the first trigger.
Thus, to solve, it is sufficient to either transfer the call to setDays after checking
var cnt = 0; $(document).on("change", "#selectMonth", function() { month = $(this).val(); if (cnt == 0) { //если выбрали месяц единожды удаляем Выберите месяц и Выберите день $("#selectMonth,#selectDay").find("option:eq(0)").remove(); } cnt++ setDays(month); })
Or not to delete the first element in the list of days, since they are already deleted before this in the setDays function
$("#selectMonth").find("option:eq(0)").remove();
Example
function setMonth() { var month = [ "январь", "февраль", "март", "апрель", "май", "июнь", "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь" ]; $.each(month, function(idx, el) { idx++ $("#selectMonth").append("<option value=" + idx + ">" + el + "</option>") }) } function setDays(month) { var date = new Date(2016, month, 0).getDate(); $("#selectDay").find("option").remove(); // setTimeout(function(){},100) for (i = 1; i <= date; i++) { $("#selectDay").append("<option value=" + i + ">" + i + "</option>") console.log(i) } } var cnt = 0; $(document).on("change", "#selectMonth", function() { month = $(this).val(); if (cnt == 0) { //если выбрали месяц единожды удаляем Выберите месяц и Выберите день $("#selectMonth,#selectDay").find("option:eq(0)").remove(); } cnt++ setDays(month); }) setMonth();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="events"> <select name="" id="selectMonth"> <option>Выберите месяц</option> </select> <select name="" id="selectDay"> <option>Выберите день</option> </select> </div>
You can also modify the code a bit using more javascript and jquery features.
function setMonth() { var months = [ "январь", "февраль", "март", "апрель", "май", "июнь", "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь" ]; months.reduce(function(selectMonth, month, index) { return selectMonth.append($('<option>').val(index + 1).text(month)); }, $("#selectMonth")); } function setDays(month) { var date = new Date(2016, month, 0).getDate(); var selectDay = $("#selectDay"); selectDay.empty(); for (i = 1; i <= date; i++) { selectDay.append($('<option>').val(i).text(i)); } } $(document).on("change", "#selectMonth", function selectMonthHandler() { $("#selectMonth option:first-child").remove(); $(document).off('change', "#selectMonth", selectMonthHandler) .on('change', "#selectMonth", function() { setDays(this.value); }); $(this).change(); }) setMonth();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="events"> <select name="" id="selectMonth"> <option>Выберите месяц</option> </select> <select name="" id="selectDay"> <option>Выберите день</option> </select> </div>
$("#selectMonth,#selectDay").find("option:eq(0)").remove();. Get the#selectDayaway from here. - Igor