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
stcforcestcforce 

select option based execution of javascript.

I need to be able to execute some Javascript based on the selection of an option from a select list, however have found that the onselect as offered with vforce doesn't work. nb. i can't use onchange because the selection is supposed to navigate to antoher page. I would appreciate any help anyone is able to give. thank you.

 

<apex:selectListvalue="{!fUpdateCurrent.Object__c}"multiselect="false" onselect="alert('select'); size="1"id="theopl">

<apex:selectOptionsvalue="{!objectPicks}"/>

</apex:selectList>

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

<apex:page id="p1" controller="Page13">

<script>

 var x='';

 var t=new Array();

</script>

 <apex:form id="f1">

<apex:pageBlock id="pb1">

<apex:pageblockSection id="pbs1" >

<apex:selectList id="pl1"  value="{!countries}" multiselect="false" onclick="check()">

            <apex:selectOptions value="{!items}"/>

            </apex:selectList>

</apex:pageblockSection>

</apex:pageBlock>

 </apex:form>

 

 <script>

 function check()

 {

var k,l=0,z;

 

    x = (document.getElementById('p1:f1:pb1:pbs1:pl1').value);

 

    alert(x);

    var y = document.getElementById('p1:f1:pb1:pbs1:pl1');

  

     //alert(y.options[3].text);

 }

 </script>

</apex:page>

 

 

 

public class Page13 {

 

 String[] countries = new String[]{};

    public String[] getCountries() {

         return countries;

            

        }

           

        public void setCountries(String[] countries) {

            this.countries = countries;      

        }

 

public List<SelectOption> getItems() {

            List<SelectOption> options = new List<SelectOption>();

            options.add(new SelectOption('US','US'));

            options.add(new SelectOption('CANADA','Canada'));

            options.add(new SelectOption('MEXICO','Mexico'));

             options.add(new SelectOption('CHINA','China'));

            options.add(new SelectOption('LONDON','London'));

            return options;

        }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

tukmoltukmol

Do you really need to do it in JS?

 

you can do it without coding a single javascript. here's a sample:

 

The VF page

<apex:page controller="TesterController">
    <apex:form >
        <apex:selectList size="1" value="{! selectedURL}" label="Navigate to URL" onchange="changePage()">
            <apex:selectOptions value="{! urlSelections}" />        
        </apex:selectList>
        <apex:actionFunction action="{! navigate}" name="changePage"/>
    </apex:form>
</apex:page>

 The controller:

public with sharing class TesterController {
	public TesterController() {
		//sample pages/websites
		this.urlSelections = new SelectOption[]{};
		this.urlSelections.add(new SelectOption('','-select website-'));
		this.urlSelections.add(new SelectOption('http://www.salesforce.com','Salesforce'));	
		this.urlSelections.add(new SelectOption('http://www.yahoo.com','Yahoo!'));
		this.urlSelections.add(new SelectOption('http://www.google.com','Google'));		
	}
	public SelectOption[] urlSelections {
		public get;
		private set;
	}
	
	public String selectedURL {
		get; set;
	}
	
	public PageReference navigate() {
		PageReference page = new PageReference(this.selectedURL);
		return page;
	}
}