Required property dependent on another field

0

I am new with the html and javascript theme, I have a query about how I could do so that depending on the value of a select & lt ;, the other property is activated in another select, I attach the two select in question, if the select 1 has the value proceeds in the select 2 disables the required property if the answer is not applicable just as it could be activated, thank you very much greetings

<label for="pais">Resultado de Gestion</label>
<select id="cmb3" name="cmb3" tabindex="6" id="mySelect" required="required">
                                    <option value="">Selecciona...></option>                                   
                                    <option value="PROCEDE">PROCEDE</option>
                                    <option value="NO PROCEDE">NO PROCEDE</option>
                                </select>
<br>
<label for="pais">Motivo no venta</label>
<select id="cmb4" name="cmb4" tabindex="7" id="mySelect" disabled>
                                    <option value="">Selecciona...></option>                                      
                                    <option value="CARGO FIJO ELEVADO">CARGO FIJO ELEVADO</option>
                                    <option value="CLIENTE CORTA LA LLAMADA">CLIENTE CORTA LA LLAMADA</option>
                                    <option value="VELOCIDAD OFRECIDA NO SE EJECUTA">VELOCIDAD OFRECIDA NO SE EJECUTA</option>
                                    <option value="MENOS MINUTOS EN LA LINEA FIJA">MENOS MINUTOS EN LA LINEA FIJA</option>
                                    <option value="SE VOLVERA A COMUNICAR">SE VOLVERA A COMUNICAR</option>
                                    <option value="PROBLEMAS TECNICOS RECURRENTES">PROBLEMAS TECNICOS RECURRENTES</option>
                                </select>
    
asked by Eduardo Vizcarra 03.03.2018 в 16:41
source

1 answer

0

I did this code to what I understood from your question, but I think that when you say change the 'required' attribute you mean to change the disabled property, but even if it is integrated into the code, I hope it works for you

JAVASCRIPT CODE

// traemos de DOM el select mediante su id
var select1 = document.getElementById('cmb3');
var select2 = document.getElementById('cmb4');
// le ponemos un evento que se ejecuta al cambiar la
// opción al primer select
select1.addEventListener('change', ()=> {
// si el select 1 esta en el índice 1 (PROCEDE)
    if(select1.selectedIndex == 1) {
// indicamos que el select 2 no es un campo requerido
      select2.attributes['required'] = '';      
// habilitamos el select2 
      select2.disabled=false;
    }else {
// indicamos que el select 2 es un campo requerido
      select2.attributes['required'] = 'required';
// deshabilitamos el select 2
      select2.disabled=true;
    }
});
    
answered by 04.03.2018 / 05:12
source