You need to sign in to do that
Don't have an account?
kumar_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>
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
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:
Controller:
This grabs the necessary data from the Custom Object.
And then:
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.
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)
}
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
Thank buddy..its working