Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery on selection disable option and make border red

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?

like image 536
user3233074 Avatar asked Jun 16 '15 09:06

user3233074


2 Answers

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/

like image 99
Tushar Avatar answered Nov 17 '22 16:11

Tushar


Simply change to

if(register_month_value === "00")
like image 29
Roko C. Buljan Avatar answered Nov 17 '22 17:11

Roko C. Buljan