SureshK
SureshK

Reputation: 123

Change first option of select

I have two date inputs & one drop down with first option "All"..

My problem is when I select dates, if both dates are same, then the first option of select should become blank.

<script type='text/javascript'>
jQuery(document).ready(function () {
    var first = jQuery('#category').find('option').first();
    jQuery('#date_to').on('change', function (e) {
    if($('#date_to').val == $('#date_from').val) {
        first.remove();
    })
});
</script>

enter image description here

Upvotes: 2

Views: 705

Answers (2)

Nishant Mendiratta
Nishant Mendiratta

Reputation: 749

As you want to remove first option you are required to put on change on both date pickers because user can pick any date and every time a user picks a date condition is executed. Well that is up to your need also.

Following is the working code.

<script type='text/javascript'>
jQuery(document).ready(function () {
    var first = jQuery('#category').find('option').first();

    $('#date_to').on('change', function (e) {
        remove();
    });

    $('#date_from').on('change', function (e) {
        remove();
    });

    function remove () {
        if($('#date_to').val() == $('#date_from').val()) {
            first.remove();
        };
    }
});
</script>

Upvotes: 2

Brijesh Bhatt
Brijesh Bhatt

Reputation: 3830

Change val to val() because it is a method. You also need to move the ) of the if statement to the correct place;

if ($('#date_to').val() == $('#date_from').val()) {
    first.remove();
}

Upvotes: 4

Related Questions