0

In following code I want to have disabled text input if select option value is 0. What's wrong?

$('select').change(function() {
  if ($(this).val() == 0)) (".xyz").attr('disabled',true);
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="type">
<option value="0"></option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<br />
<input type="text" class="xyz" name="xyz" />
Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
zaziD
  • 15
  • 1
  • 6

1 Answers1

1

There are syntax errors in your code:

Uncaught SyntaxError: Unexpected token )

You misspelled $ (jQuery selector) with a ).

Furthermore, you haven't managed the case in which the "type" select has a non-0 value.

$("select").change(function() {
  if ($(this).val() == 0) {
    $(".xyz").attr("disabled", "disabled");
  } else {
    $(".xyz").removeAttr("disabled");
  }
}).trigger("change");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="type">
  <option value="0"></option>
  <option value="1">1</option>
  <option value="2">2</option>
</select>
<br />
<input type="text" class="xyz" name="xyz" />
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34