+ Start a Discussion
DrawloopSupportDrawloopSupport 

Get value from the selected selectOption in a selectList

Suppose I have a select list as shown in this visualforce code:

 

<apex:selectList id="type" size="1" value="{!type}" onchange="{!test}"> <apex:selectOption itemLabel="Type1" itemValue="Type1" /> <apex:selectOption itemLabel="Type2" itemValue="Type2" /> <apex:selectOption itemLabel="Type3" itemValue="Type3" /> </apex:selectList>

 

 and I want to alert the value of the selected selectOption from the selectList as shown in this apex code:

 

public string selectedType; public string getType() { return selectedType; } public void setType(string value) { selectedType = value; } public string getTest() { return 'alert("'+selectedType+'");'; }

 

 Now, I'm guessing the default getter and setter of the type value of the selectList does not work as I thought it would since this apex code simply alerts "null".

Does anyone know how to make this work or how to get the value of the selected selectOption within the selectList?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
aebenaeben

Try action region.

 

I have not tried it myself. BUt, I am aware that action region does something similar.

All Answers

hisrinuhisrinu

http://www.salesforce.com/us/developer/docs/pages/index.htm

 

Checkout this link

Using this link you can get the value and then you can show the alert to user using java script.

DrawloopSupportDrawloopSupport

Srini,

 

This link is the main page of the vf developer's guide instead of a specific page. I have done searches here and cannot find a sufficient example. The example for selectList uses a dataList to display the selected selectOption values. It does not explain nor show how one would use the selected selectOption value(s) in the corresponding controller. I'm having a hard time extrapolating what I need from the documentation.

 

Could you provide a direct link to something you think is useful/helpful? Or provide an example?

 

I'm going to continue searching on google and such.

 

Thanks!

DrawloopSupportDrawloopSupport

Still not having any luck. I've tried using apex:actionSupport event="onchange" with rerender and action attributes. I've tried using apex: param inside the actionSupport and inside a commandButton. I just can't seem to get access to the value of the selected selectOption from my apex code.

 

Can anyone help me?????

hisrinuhisrinu

Hey I am sorry,

 

In that VF Guide, check out the section Using JavaScript to Reference Components

 

It will help you out.

 

DrawloopSupportDrawloopSupport
Perhaps I did not explain the problem correctly. The only reason I was alerting the value using javascript was to see if I was getting the right value in apex. I need to use the value that is selected in apex and I can't figure out how to get it. When I use the standard getter and setter for the value of the selectList, that value is always null in apex. If I check to see if the value is null then set it to a particular value, the value is always that value even if the selected option changes. Does this make sense? It's not the javascript part that I need help with, but the apex-selectList part.
aebenaeben
If I remember correctly, onchange will not submit the form. The setters will be called only when the form gets submitted (thats is calling an action).
DrawloopSupportDrawloopSupport
Is there any way to submit the form with a rerender option so that the entire form does not get submitted but the setter gets called? When my selectList value changes, I need to rerender a part of the visualforce page based on the selected value. How are other developers changing options based on a selectList?
aebenaeben

Try action region.

 

I have not tried it myself. BUt, I am aware that action region does something similar.

This was selected as the best answer
bmontgombmontgom

Here is an example using a radiobutton.  First the visualforce:

 

                <apex:pageBlockSection title="Ticket Tier">
                    <apex:selectRadio value="{!tickettier}">
                        <apex:selectOptions value="{!Tiers}"/>
                    </apex:selectRadio>

                  <apex:pageBlockButtons location="bottom">
                    <apex:commandButton action="{!setticket}" value="Finish"/>
                </apex:pageBlockButtons>
 

At least for radio buttons I think you need to have an action command button to get the value into the variable.

 

The apex code to set the selectOptions is this:

 

    String tickettier = null;

 

    public List<SelectOption> getTiers() {
        List<SelectOption> Tiers = new List<SelectOption>();
        if (camp.t1_name__c <> null) {
        Tiers.add(new SelectOption(camp.t1_name__c, camp.t1_name__c));
        }
        if (camp.t2_name__c <> null) {
        Tiers.add(new SelectOption(camp.t2_name__c, camp.t2_name__c));
        }
        if (camp.t3_name__c <> null) {
        Tiers.add(new SelectOption(camp.t3_name__c, camp.t3_name__c));
        }
          return Tiers;
      }

    public String gettickettier() { return tickettier; }

    public void settickettier(String tickettier) { this.tickettier = tickettier; }     

 

The Visualforce guide has good examples of how to get the page to refresh once you have the selectOption value.

DrawloopSupportDrawloopSupport

aeben,

 

Thanks a lot! That does the trick.