Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird behavior in "change" event

I have 2 select tags, where based on the value selected in first select I'm show/hide the option in 2nd select tag which is working perfectly. However today I noticed a weird behavior.

enter image description here

When I tried to change the option in 2nd select using keyboard down arrow, it is still showing the hidden option. Really confused. Please share your thoughts.

Here is the fiddle, where you can reproduce the above bug as I mentioned in the picture.

JS:

$('#AboveOrBelow').on('change', function(){
    if($(this).val() === '1') {
        $('#InitialLeakSource option[data-aob=Above]').show();
        $('#InitialLeakSource option[data-aob=Below]').hide();
    }
    else if($(this).val() === "2"){
        $('#InitialLeakSource option[data-aob=Above]').hide();
        $('#InitialLeakSource option[data-aob=Below]').show();
    }
    else {
     $('#InitialLeakSource option').show();
    }
});

PS: The above picture is not clear in 100%, please zoom for a clear vision.

like image 720
Praveen Avatar asked Feb 16 '26 21:02

Praveen


1 Answers

just add the below line at the end of you function

$('#InitialLeakSource').val('');

please check fiddle

like image 191
Mohammad Ismail Khan Avatar answered Feb 18 '26 11:02

Mohammad Ismail Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!