0

I have a page with a select and a textbox. I want keep disabled the textbox if the option selected is different than "Altro". How can I do? I tried something by my textbox is always disabled.

HTML:

<p>Partenza:    <select id="selPartenza">
                    <option class="opt" value="0">Altro</option>
                    <option class="opt" value="1">Opt1</option>
                    <option class="opt" value="2">Opt2</option>
                </select>
                </p>                

                <textarea id="textP" placeholder="Aggiungi qui l'indirizzo di partenza se non lo trovi mell'elenco precedente."></textarea>

Javascript:

$("#selPartenza").each(function(i){
    $(this).click(function () {
        if(i==0) { //1st option
            $("#textP").attr("disabled", "disabled");                              
        } else {
            $("#textP").removeAttr("disabled");          
        }
    });
});

Obviously I'm not able to select the options in my script. How can I do? Thank you for your answers.

Ennio
  • 153
  • 2
  • 11
  • you need to use `.prop('disabled', true)` when setting disabled, also bind to a change event for the select (rather than binding a click to what I think is you're trying to do each option) – Pete Jun 14 '18 at 15:32
  • Possible duplicate of [jQuery .attr("disabled", "disabled") not working in Chrome](https://stackoverflow.com/questions/6048022/jquery-attrdisabled-disabled-not-working-in-chrome) – Pete Jun 14 '18 at 15:32
  • @Ennio My answer on your last question, https://stackoverflow.com/questions/50912959/php-take-the-value-of-a-select-sum-from-database/50913831, didn't resolve the issue? – user3783243 Jun 18 '18 at 16:34

1 Answers1

3

You need to use $('#selPartenza').on('change' and prop('disabled', true)

$('#selPartenza').on('change', function(){
  if(this.value == 0)
    $('#textP').prop('disabled', true)
  else
    $('#textP').prop('disabled', false)
})

$('#selPartenza').change()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Partenza:    <select id="selPartenza">
                    <option class="opt" value="0">Altro</option>
                    <option class="opt" value="1">Opt1</option>
                    <option class="opt" value="2">Opt2</option>
                </select>
                </p>                

                <textarea id="textP" placeholder="Aggiungi qui l'indirizzo di partenza se non lo trovi mell'elenco precedente."></textarea>