How valid the state of both select at the same time!

1

I have a problem validating the indexes of the selected options in a select since the event only runs in the select utlimo, since the selected index == 0, it is as if the for would not work

the button must be activated only if both select are in an index other than 0 but when selecting index 0 in select number 1 and index 1 in select 2 the button is still active.

let btn = document.getElementById("btn");
let list = document.getElementsByClassName("options");
btn.disabled = true;

for (item of list){
  
  item.addEventListener('mouseout', function(){
    if(item.options.selectedIndex === 0){
        btn.disabled = true
    }else if (item.options.selectedIndex !== 0){
        btn.disabled = false
    
 } 
}, false)
}
<select class="options"name="" id="">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <select class="options"name="" id="">
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
    </select>
    <button id="btn">Submit</button>
    
asked by SERGIO 05.05.2018 в 23:46
source

2 answers

1

The problem is that when the mouseout event of the select element is executed, it is only evaluating only one element at a time, so it never verifies that the two elements have selected indices other than 0.

For this you must go through all the elements and verify that no element has selected the index 0 of the list of options, here I show you an example of how it would be when validating all the elements:

Eye: You are using Ecmascript 6 syntax, so you should be aware that in older browsers this solution may have problems.

let btn = document.getElementById("btn");
        let list = document.getElementsByClassName("options");
        btn.disabled = true;

        for (item of list) {
            item.addEventListener('mouseout', function () {
                let numberOfValidElements = 0;
                Array.from(list).forEach(element => {
                    if (element.options.selectedIndex !== 0) {
                        numberOfValidElements++;
                    }
                });

                btn.disabled = (numberOfValidElements === list.length ? false : true);
            });
        }
<select class="options" name="" id="">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <select class="options" name="" id="">
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
    </select>
    <button id="btn">Submit</button>
    
answered by 06.05.2018 / 01:36
source
1

Well, according to what I understood, I made you a demo.

In the html you assign an id to each select (different)

<select class="options"name="" id="select1">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>
<select class="options"name="" id="select2">
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
</select>
<button id="btn">Submit</button>

Then with javascript you check with the onchange event if any of them has changed.

let select1 = document.getElementById('select1');
let select2 = document.getElementById('select2');
let btn = document.getElementById('btn');

//Por defecto 
btn.disabled = true;

select1.addEventListener('change', () => {
        select1.selectedIndex != 0  && select2.selectedIndex != 0? btn.disabled= false: btn.disabled= true;
})

select2.addEventListener('change', () => {
        select1.selectedIndex != 0  && select2.selectedIndex != 0? btn.disabled= false: btn.disabled= true;
})

I hope it has helped you!

    
answered by 06.05.2018 в 01:12