How to enable or disable an input according to the option of a select html by javascript?

0

I want an input to start disabled and when the person selects for example a certain select option the input is enabled.

    
asked by anonymous 22.01.2018 / 13:43

2 answers

2

Well you can take this example as the basis:

function verifica(value){
	var input = document.getElementById("input");

  if(value == 2){
    input.disabled = false;
  }else if(value == 3){
    input.disabled = true;
  }
};
<!DOCTYPE html>
    <html>
    <head>
    <title>Page Title</title>
    </head>
    <body>
    <input type="text" name="input" id="input" disabled>
    <br><br>
    <select id="options" onchange="verifica(this.value)">
    <option value="1" selected>1 - Não interfere</option>
    <option value="2">2 - Habilita</option>
    <option value="3">3 - Desabilita</option>
    </select>
    </body>
    </html>
    
22.01.2018 / 14:10
0

Selecting option = Voley input will be enabled.

Use getElementById to identify each element within the form.

To retrieve the value of the selected option

document.getElementById("jogos").value; 

Having this value enable or disable the input.

 function validarForm() { 
       var optionSelect = document.getElementById("jogos").value;

       if(optionSelect =="3" ){ 
           document.getElementById("btn").disabled = false;
       }else{
           document.getElementById("btn").disabled = true;
       }
}
    <form method="post" action="https://pt.stackoverflow.com/"> 
    <select name="jogos" id="jogos" onchange="validarForm()">
    <option value=''>Selecione o esporte</option>
    <option value='1'>Futebol</option>
    <option value='2'>Tenis</option>
    <option value='3'>Voley</option>
    </select>
    <input type="text" id="btn" disabled>
    </form> 
    
22.01.2018 / 18:26