0

Basically what I'm trying to do is self-explanatory in the title. Here's a snippet of code which enables the dropdown/select so it can be changed via client, but it will not disable after change:

function setLocVals(passedVal) {

                $('#DropDownList1').removeAttr("disabled");                    
                $('#DropDownList1').val(passedVal);
                $('#DropDownList1'').attr("disabled", "disabled");
}

Is this even possible? Or am I looking at this completely the wrong way? I could change to a textbox, but this is a dropdown containing US states. If there's a record for the user then the selection is restricted for the user, if no record exists, then they can select.

Andrew
  • 253
  • 2
  • 14
  • When you `removeAttr()` the attribute is no longer there. Instead, you wan to use `.prop('disabled', false)` or `.prop('disabled', true);` – Ohgodwhy Apr 18 '13 at 21:35
  • Pretty sure you don't need to mess with the `disabled` property just to change the selected option through JS. – Fabrício Matté Apr 18 '13 at 21:36
  • Possible duplicate with http://stackoverflow.com/questions/7703241/enable-disable-a-dropdownbox-in-jquery – griegs Apr 18 '13 at 21:37
  • Not a duplicate as the link you reference is an input checkbox and select. This is a select and a select. – Andrew Apr 19 '13 at 16:15
  • @Ohgodwhy tested by using .prop in the fashion you listed... Same result... – Andrew Apr 19 '13 at 18:09

1 Answers1

0

Just put a condition when you have to disable. See this fiddle

Use this approach.

First set the value in option.

Then retrieve the current value of the option and check if it is set to the one than you have passed then put the condition to disable else not

 function setLocVals(passedVal) {


            $('#DropDownList1').val(passedVal);

             var value = $('#DropDownList1').val();
             if(value == passedVal){
                    $('#DropDownList1').attr("disabled", "disabled");
                }
        }
NullPointerException
  • 3,732
  • 5
  • 28
  • 62
  • Doesn't address the fact that you can't change the value of the dropdown from the client side if the control/element is disabled. Dropdownlist begins as disabled, value is to be changed so it is enabled, and then disabled again after value is changed. – Andrew Apr 19 '13 at 16:20
  • but as you said that if a value is present it has to be disabled so that user cannot change that and if the value is not present then it should be enabled for user to change – NullPointerException Apr 19 '13 at 16:23
  • No, I might've misphrased that. I'm only working under the 'there are records' scenario. I stipulated the two situations because I didn't want someone to answer "Well, why don't just use a textbox?" There are 2 dropdowns one enabled, one disabled - I need to change the 2nd dropdown based on the 1st dropdown selection and the dropdown needs to remain disabled. I hope that's clearer. I apologize for clouding the issue in the original post. – Andrew Apr 19 '13 at 23:24
  • Look at the updated [fiddle](http://jsfiddle.net/VfuCc/2/) and let me know if that worked or not so that I can update the answer with correct solution. – NullPointerException Apr 22 '13 at 03:06
  • That's the behavior I'm looking for, but I tried something similar with my code. I tried to implement that code into my own, but it just won't do it. I had it databound, and then I tried static. I do use a javascript object, but all other values are being popped fine (even though they are textboxes). Maybe I should just turn this to a textbox. I'm dumbfounded as to why it won't change when disabled, but it will when enabled.... – Andrew Apr 23 '13 at 21:39