function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mavsmavs 

Page validation

Hi

what am i doing wrong here??

I am trying to pass selected value of selectlist to javascript function in the commandLInk onclick event

 

 when the command link is clicked, in the alert it is showing as undefined....

 

How to pass the selected list value here? Please advise

 

<apex:page controller="option_test">
<apex:form 
 <apex:pageBlock>
 <apex:selectList id="ch" value="{!num}" multiselect="false" size="1">
            <apex:selectOption itemValue="" itemLabel=""/>
            <apex:selectOption itemValue="01" itemLabel="01"/>
            <apex:selectOption itemValue="01" itemLabel="02"/>
  </apex:selectList>
<apex:CommandLink onclick="return validate()">click</apex:CommandLink>                 <script language="javascript">
                       function validate()               
                        {                   
                            try 
                            {
                                alert("Hi "+document.getElementById("{!$Component.ch}").value)
                                return false;
                            }
                            catch(e)                   
                            {                       
                                alert(e);                       
                                return false;                   
                            }
                         }
        </Script> </apex:pageBlock> </form> </page>

 

public class option_test
{
  public String num{get;set;}
}

 

 

bob_buzzardbob_buzzard

I've tried your code in IE8, firefox and chrome and it works as expected once I'd fixed a couple of things:

 

 

  • Your apex:form tag is missing the left angled bracket to close the tag
  • The final tag of the page should be closing apex:page, not just page
  • The final ItemValue is 01, but the label is 02.

 

mavsmavs

thanks Bob..

 

It worked!!

 

question: it should be same for selectRadio as well correct? the code below for selectRadio isnt working

 

Please advise

 

<apex:page controller="option_test">
<apex:form> 
 <apex:pageBlock>
 <apex:selectRadio id="ch" value="{!num}" >
            <apex:selectOption itemValue="01" itemLabel="01"/>
            <apex:selectOption itemValue="02" itemLabel="02"/>
  </apex:selectRadio>
<apex:CommandLink onclick="return validate()">click</apex:CommandLink>  
<script language="javascript">                       
function validate()                                        
{                                                
try                              
{                                
alert("Hi "+document.getElementById("{!$Component.ch}").value)                                
return false;                            
}                            

catch(e)                                                
{                                                        
alert(e);                                                        
return false;                                                
}                          
}        
</Script>
               
</apex:pageBlock>
</apex:form>
</apex:page>

 

public class option_test 
{
public String num{get;set;}
}

 

mavsmavs

hi - Any word on this?