Here is a snippet of my jQuery code written in Coffeescript for a Rails 3.1.1 application:
$("#type_well_volume").change(->
unless $(this).val() == ""
$("#type_well_abandonment_rate").val("").attr "disabled", true
$("#type_well_case_life").val("").attr "disabled", true
else
$("#type_well_abandonment_rate").attr "disabled", false
$("#type_well_case_life").attr "disabled", false
).trigger "change"
$("#type_well_case_life").change(->
unless $(this).val() == ""
$("#type_well_abandonment_rate").val("").attr "disabled", true
$("#type_well_volume").val("").attr "disabled", true
else
$("#type_well_abandonment_rate").attr "disabled", false
$("#type_well_volume").attr "disabled", false
).trigger "change"
$("#type_well_abandonment_rate").change(->
unless $(this).val() == ""
$("#type_well_volume").val("").attr "disabled", true
$("#type_well_case_life").val("").attr "disabled", true
else
$("#type_well_volume").attr "disabled", false
$("#type_well_case_life").attr "disabled", false
).trigger "change"
As you can see, my intent is to allow only one of these three fields to be populated by the user, and as soon as the user enters a value for any one of these, grey-out (i.e., make read-only) the other two fields. Additionally, when the DOM loads, I want the right event handler to trigger such that the field that has a value is open for edits and other two are greyed out.
The code shown above does not work reliably. Thanks in advance for your help
I made this jsfiddle, is this equivalent functionality that you wanted?
http://jsfiddle.net/UkwFx/
Here is jsfiddle when one field is pre filled:
http://jsfiddle.net/UkwFx/1/