Search code examples
javascripthtml-selectgetelementbyid

how do i check for specific values from a dropdown list using this.value in javascript


I have a dropdown select list with about 10 options connected with a hidden div which supposed to be displayed when only any of specific 3 options being chosen using javascript code

document.getElementById('item').addEventListener('change', function () {
var style = (this.value == "661056067" or this.value == "571855424") ? 
'table-row' : 'none';
document.getElementById('hidden_div').style.display = style;
});

I tried the code showed above as I wanted this.value function to be equal to more than one value but it didn't work. So whats the right way to make ot work. Kindly be noted I am not good at all with javascript. Thanks for your help


Solution

  • Instead of or use ||

    document.getElementById('item').addEventListener('change', function () {
        var style = (this.value == "661056067" || this.value == "571855424") ? 
        'table-row' : 'none';
        document.getElementById('hidden_div').style.display = style;
    });
    

    You can also use includes.

    document.getElementById('item').addEventListener('change', function () {
        var style = ["661056067", "571855424"].includes(this.value) ? 'table-row' : 'none';
        document.getElementById('hidden_div').style.display = style;
    });