1

I want to clear multi picklist value when it is disabled. I'm using like this but it's not working.

<apex:page standardController="Contact">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<script>  
 function me(obj)
{ 
        var SelecOptionsStr = '';
        var selectList = obj;

        for(var i = 0; i < selectList.options.length; i++)
        {                 
            if(selectList.options[i].selected)
            {
                if(selectList.options[i].text=='Media')
                {
                    $('.one').prop('disabled',false);
                }
            }
            else
            {
                if(selectList.options[i].text=='Media')
                {
                    $('.one').prop('disabled',true);
                    $('.one').val("");//Here i'm expecting to clear the options. Not sure its just an assumption.
                } 
            }                      
        }
}
   <apex:form >
   <apex:inputField value="{!Contact.Type__c}"  onchange="me(this);"/>
  <apex:inputField value="{!Contact.multiPick__c}" styleClass="one"/>
  </apex:form>
 </apex:page>

When Type__c = Media, I'm enabling MultiPick__c field with choosing one value in it. later on if I don't want the "Media" as the option in Type__c, then i'm disabling the multipick__c field. Along with disable, i would like to clear the options that i have already selected in Multipick__c field.

Can any one suggest me.

  • Can you explain your requirement more? This "clear multi picklist value when it is disabled" doesn't really correspond to your code where you have two fields and seem to be trying to disable one based on a value selected in another. I would have expected that you would be comparing selectList.val() with 'Media' instead of looping over all its options. – Keith C Oct 7 '14 at 7:43
  • @Keith C, I forgot to add my requirement. My question was edited. please notice it. – shirisha Oct 7 '14 at 7:57
  • Note that jQuery's val handles the multi-select case by returning an array and allowing an array to be set. – Keith C Oct 7 '14 at 8:07
  • Little different than what you asked for... You cannot enable a component by setting disabled = 'false'. You need to remove the 'disabled' attribute from that element to enable it. – ajinkyah Mar 4 '16 at 7:37
0

This is made more complicated by the way Visualforce renders two select lists for multi-select picklists. The code below clears the options by replacing the HTML with the HTML present when the page first rendered:

<apex:page standardController="Contact">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<script>
var j$ = jQuery.noConflict();
j$(document).ready(function() {
    var multiRow = j$('.multi').parents('.multiSelectPicklistRow');
    var initialHtml = multiRow.html();
    j$('.single').change(function() {
        var isMedia = j$(this).val() === 'Media';
        if (isMedia) {
            multiRow.html(initialHtml);
        }
        j$('.multi').prop('disabled', isMedia);
    });
});
</script>
<apex:form >
    <apex:inputField value="{!Contact.TestPicklist__c}" styleClass="single"/>
    <apex:inputField value="{!Contact.TestMultiPicklist__c}" styleClass="multi"/>
</apex:form>
</apex:page>

Automatically selecting a value when the list is re-enabled wouldn't be easy either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.