+ Start a Discussion
kumar_devkumar_dev 

Problem with populating selectlist options using javascript

Hi everybody,
I am trying to populate selectlist options through onload javascript function, but some how this is not working. I have pasted the code below that iam working on, could any body please suggest any changes to make it working.
<apex:page >
<script> window.onload = list1;
function list1()
{
var list=document.getElementById('listchosen');
for(var i=0;i<10;i++)
list.add(new Option('xxx','xxx'));
}
</script>
<apex:outputPanel id="result"> <apex:form >
<b>Please select :</b>
<apex:selectList id="listchosen" multiselect="false" size="1"> </apex:selectList>
</apex:form>
</apex:outputPanel>
</apex:page>

 

Best Answer chosen by Admin (Salesforce Developers) 
prafsprafs

Hi Kumar,

 

You have to modify your list1 method.

1. SFDC generated ids at run time for the apex tags. Please use correct Id of the apex:selectList.

2. You should add the Option to options list of the html element.

 

Please check the below code -

function list1()

{

var list=document.getElementById('j_id0:j_id2:listchosen');

for(var i=0;i<10;i++)

list.options.add(new Option(i,i));

}

 

Hope this helps,

-P

All Answers

GobbledigookGobbledigook

I'm not too good with Javascript, so maybe you'll be able to translate, or maybe it'll point you in the right direction. Using Apex Code, this is how I got it to work:

 

<apex:selectList value="{!contacts}" id="AccountCheck" >
     <apex:selectOptions value="{!items}"/>
</apex:selectList>

 

Controller:

 

public String[] getContacts() { return contacts; }
public void setContacts(String[] contacts) { this.contacts = contacts; }

This grabs the necessary data from the Custom Object.

And then:

 

 

 

public List<SelectOption> getItems() 
    {
        //Fill the Contact list from the List of SelectOption in the VF Page and Map it for later usage
        c = new List<Contact>([Select Name, Id from Contact where Account.Id = :acc.Id]);
        getContacts = new Set<Id>();
        options = new List<SelectOption>();
        for(Integer i = 0; i < c.size(); i++)
        {
            getContacts.add(c[i].Id);
            options.add(new SelectOption(c[i].Id,c[i].Name));
        }
        showContacts = new Map<Id, Contact>([select Name from Contact where Id in :getContacts]);
        return options;
    }

 

 

Just reaching here, but maybe:

<apex:selectList id="TheList" multiselect = "false" value="listchosen" > </apex:selectList>

 

Or maybe:

<apex:selectList id="TheList" multiselect = "false">

<apex:selectOptions value="listchosen" />

</apex:selectList>

 

Hope this helps in any way, or at least points you in the right direction.

 

 

 

 

Pradeep_NavatarPradeep_Navatar

Tryout this sample code to get picklist value through javscript :

 

           function list1()

           {

           var opt = document.createElement("option");

           opt.text = 'demo';

           opt.value = 'demo';

           document.getElementById('pageid:formid:listchosen').options.add(opt)

            }

prafsprafs

Hi Kumar,

 

You have to modify your list1 method.

1. SFDC generated ids at run time for the apex tags. Please use correct Id of the apex:selectList.

2. You should add the Option to options list of the html element.

 

Please check the below code -

function list1()

{

var list=document.getElementById('j_id0:j_id2:listchosen');

for(var i=0;i<10;i++)

list.options.add(new Option(i,i));

}

 

Hope this helps,

-P

This was selected as the best answer
kumar_devkumar_dev

Thank buddy..its working