2

Can someone suggest me a way to display an alert message onchange of picklist values(Different alert message, for different picklist values). Below is the sample code that I'm using, but the javascript isn't working. Any help is much appreciated. Thanks.

VisualForce:

<apex:pageblockSectionItem> <apex:inputfield value="{!picklist1}" onchange="reset();"> </apex:pageblockSectionItem>

    <apex:actionfunction name=reset action="{!check}" rerender="pageBlock">

    <script>
     <apex:outputpanel rendered={!show}>
        var val = {!picklist1}
         if(val == 'yes')
             {
            alert('{!mes}');
                 }
     </apex:outputpanel>
    </script>

Apex Class:

class{
public boolean show{get;set;}
public String mes{get;set;}
public void check(){    
    if(picklist1 == 'yes')
    { 
      show = true;
      mes  = 'you selected yes';
    }

}
}
  • Give the output panel an ID <apex:outputpanel rendered="{!show}" id="alert"> and rerender it in the actionfunction <apex:actionfunction name=reset action="{!check}" rerender="alert">. Try that – Boris Bachovski Jul 31 '14 at 3:54
  • Bachovski: I'm rerendering the pageblock in actionfunction, I cannot do multiple rerenders, is there any other way?. Updated my code. – cartman Jul 31 '14 at 3:59
3

You can do all that check in Javascript and then call your apex method (if you have to).

Let's assume this is your picklist:

<apex:selectList id="myPicklist" size="1" value="{!selectedValue}" onchange="myPicklistChanged();">
    <apex:selectOptions value="{!myPicklistOptions}" />
</apex:selectList>

And you have your action function (only if you need to call your apex method)

<apex:actionfunction name="reset" action="{!check}" rerender="pageBlock" />

Finally your javascript function that is called from the onchange event on your picklist:

<script>
    function myPicklistChanged()
    {
        var myPicklistElement = document.getElementById('{!$Component.myPicklist}');
        var myPicklistValue = myPicklistElement.options[myPicklistElement.selectedIndex].value;

        if (myPicklistValue == 'value 1')
        {
            alert('value 1');
            // if you don't want to call your apex method
            // return;
        }
        else if (myPicklistValue == 'value 2')
        {
            ...
        }

        // if you still want to call your apex method after this check
        // check();
    }
</script>
| improve this answer | |
0

I had the same issue recently, this worked for me...

VF:

<apex:selectList id="type1" value="{!lab.Country__c}" size="1" onchange="countryChanged(this)">
    <apex:selectOptions value="{!CountryTypes}"/>
</apex:selectList>

JS:

<script>
function countryChanged(selectedCountry){
var country = selectedCountry.options[selectedCountry.selectedIndex].value;
alert(country);
}
</script>

APEX:

public list <SelectOption> getCountryTypes (){

List <SelectOption> options = new List<SelectOption>();
Schema.DescribeFieldResult fieldResult = Labels__c.Country__c.getDescribe();
List <Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for( Schema.PicklistEntry f : ple){
    options.add(new SelectOption(f.getLabel(), f.getValue()));
}       
return options;
}
| improve this answer | |

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.