JamieVanderlay
JamieVanderlay

Reputation: 3

When box is checked make a drop down readonly or uneditable in Rails

I have a drop down box in ruby on rails that allows a date to be entered. And there is a box beside it to indicate that no date will be entered.

When this box is ticked it changes the default value of the date to nil. However the user can still enter a date on the drop down boxes.

When the box is ticked I want the drop down to be readonly or uneditable how can I do this?

EDIT

function BlankOutStartDates() { 
if     (document.getElementById("service_unknown_start_date").checked == true) 
{ 
 document.getElementById("service_start_date_1i").selectedIndex = 0;     
 document.getElementById("service_start_date_2i").selectedIndex = 0;
 document.getElementById("service_start_date_3i").selectedIndex = 0; 
} 
} 

I want to add the addition javascript to make the drop down box read only if the box is ticket

Upvotes: 0

Views: 844

Answers (1)

CambridgeMike
CambridgeMike

Reputation: 4622

I would actually do this in jQuery. Suppose your checkbox has class "no-date", and your select boxes all of the class "date-select". Then you could do this:

jQuery(".no-date").change(function(){
   //Check if the box is checked
   if(this.is(':checked')){
      jQuery(".date-select").attr("disabled", "true");
   } else {
      jQuery(".date-select").removeAttr("disabled");
   }
});

You'll want to double check "this.is(':checked')" is correct, but I think that will work.

This uses the "disabled" parameter of select tags: http://www.w3schools.com/tags/tag_select.asp

Upvotes: 2

Related Questions