0

I want to disable a multipicklist field, depending on other picklist field value.

if picklist__c is 'Inactive', then disable multipicklist__c. I used javascript but no luck.

document.getElementById('multipcklist').disable=true;

is not working.

I also tried :

document.getElementById("{!$Component.multipcklist}").disable=true;

still no luck.

please provide me any example so that i can implement the same in my vf page.

  • It's disabled rather than disable (e.g. myElement.disabled=true). The issue may be that you are not getting the element correctly. Have a look here to figure out how to start debugging your javascript. If you are still stuck then update your question to include the picklist field you are trying to disable and the how the javascript gets executed. – BarCotter Oct 01 '14 at 12:20

3 Answers3

2

You can achieve this as like below :

<apex:page controller="testListController">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<apex:form >
    <apex:inputField value="{!acc.CustomerPriority__c}" styleClass="first"/>
   <apex:inputField value="{!acc.testmulti__c}" styleClass="second"/>
</apex:form>
    <script>
$(document).on('change','.first',function(){
  console.log($('.second'));
$('.second').prop('disabled','disabled');
});

</script>
</apex:page>
Matt Lacey
  • 25,618
  • 7
  • 66
  • 149
Eric
  • 450
  • 1
  • 4
  • 13
  • 2
    In your answer you use JQuery, not only Javascript. – SF_user Oct 01 '14 at 12:21
  • 1
    Thanks for reply. As i'm new to jquery can you please tell me how can i achieve this based on the picklist field values. If picklist__c is 'Inactive', i want to disable it. – Ravali Pothineni Oct 01 '14 at 12:38
  • @SF_user We can bind event on select item for change event in javascript by using addEventListener but can't disable select list in an easy way so I used jquery. And problem was "Disable a multipicklist depending on other picklist value". This problem is solved by my answer :) – Eric Oct 01 '14 at 12:42
  • $(document).on('change','.first',function(){ if($(this).val() === 'Inactive'){ $('.second').prop('disabled','disabled'); } });

    This will solve your problem. Don't forget to mark as answer to my reply :)

    – Eric Oct 01 '14 at 12:46
  • Yes but in the question, OP said I used javascript. That's why I answered with a JS solution and I commented your answer ;) – SF_user Oct 01 '14 at 12:46
  • No issue @SF_user. We are here to help each other and learn by each other. My first motive is to solve query :) I am just queries that why did you down vote my answer ? :( – Eric Oct 01 '14 at 12:48
  • I didn't vote down your answer ;) just left a comment ! Someone voted down but it's not me ! – SF_user Oct 01 '14 at 12:50
  • This way of down voting is always demotivate new users like me :( – Eric Oct 01 '14 at 12:50
  • 1
    I upvoted your answer because I think your answer is available ! – SF_user Oct 01 '14 at 12:57
  • 1
    @sales_lion You're back in the positive now ;) I did remove the last couple of lines from your answer though. On SFSE it's assumed that people will comment if something doesn't work, and accepting answers is a core mechanic of the site. You shouldn't ask people to do it! – Matt Lacey Oct 01 '14 at 23:50
0

Try with disabled (with a 'd'), like this :

document.getElementById('{!$Component.multipcklist}').disabled=true;
SF_user
  • 3,616
  • 10
  • 41
  • 56
-1

As per the Question the Picklist values you are referring to should control display of second picklist field(multi-select).

We can achieve this using javascript, JQuery, ActionFunction, ActionSupport.

As part of Maintenance: The Question comes what to use when, for ex: What if the picklist values of the first picklist changes in the future? If we use javascript or Jquery we need to change the code as per the changed values every time using these values in a string array.

What I think as better solution is using Action support Tag to act on changing the values in picklist and a custom settings object with values in picklist1 for which the second picklist field has to get disabled. use this custom settings object in controller to update a boolean variable true or false and use it in second picklist tag to hide.

So, when ever the values in first picklist field changes, you can update the custom settings with those values so that you no need to change the code every time.

If the first picklist field values not gonna change then its always good to go with client side scripting using javascript or JQuery to implement as given above.

Ki Vee
  • 11
  • 3