There is a lot written about enabling/disabling elements by jQuery, but I have a little confusion. I try to use a select and a single input field, if select is used, the field have to be disabled and vice versa.
For example
<span>Choice a year from the list</span>
<select id="selYear" name="selYear" >
<option id="op-0">Choice a year</option>
<option id="op-1" >1893 </option>
<option id="op-2" >1894</option>
</select>
<span>Or type a new one</span>
<input id="year_single" name="year_single" type="text" >
(Of course there must be a input filter.) To disable the input field and enable it, if the first option is chosen was easy. If some body inquires how, there is the code:
$("#selYear").change(function() {
if(this[0].selected){
$("#year_single").attr("disabled",false);
}
else
$("#year_single").attr("disabled",true)
})
)
But the opposite case works not how I expected. I tried:
$("#year_single").change(function() {
if($(this).val().length == 0){
//alert("aaa");
$("#selYear").attr("disabled",false);
}
else
$("#selYear").attr("disabled",true)
})
This works, but the select becomes disabled after pressing Insert key, or after clicking on it, I expected to be disabled exactly after first inserted digit in it.
Probably the change()
function isn't the right choice in such a case?