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
revathyrevathy 

how to add values in select options using Java script?

For Example:java script result getting below format like that,
var result=sdfsd,sdfsdf,sfsdfsd,sfsdfsd,ertert
this values need add my options below format
sdfsd
sdfsdf
sfsdfsd
sfsdfsd
ertert
see my attached screen what i got result.
User-added image

my javascript code
===============
<script type="text/javascript">
  
function selectPosition(id){

var pos1 = document.getElementById(id).value;

Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.AccountRemoter.getRemotePieData}',
            pos1,
         function(result, event){
      
                if (event.status && result.constructor === Array) {
                  
                        var opt = result.length;
                         var ListOfopt = result;
                         alert(ListOfopt);
                var str2 =  new Array(ListOfopt);
                   alert(str2);
                  
    
       
   
          var x = document.getElementById("mySelect");
                       
                        var option = document.createElement("option");
                       
                        option.text = result;
                       
                        x.add(option);
      
                 
                      
                     
                } else if (event.type === 'exception') {
                    document.getElementById("responseErrors").innerHTML =
                        event.message + "<br/>\n<pre>" + event.where + "</pre>";
                } else {
                    document.getElementById("responseErrors").innerHTML = event.message;
                }
            },
            {escape: true}
        );
         document.getElementById("mySelect").options.length=0
}
    </script>

please anyone help on this
revathyrevathy
i need values in option:
sdfsd
sdfsdf
sfsdfs
sfsdfsd
Vikash TiwaryVikash Tiwary
Hi revathy,

make an array say options.

selectOptions = '<option value="none">--None--</option>'
for(var i = 0; i < options.length; i++)
{
                selectOptions += '<option value="'+options[i]+'">'+options[i]+'</option>';
}        
jq("#terminationReason").html(selectOptions); //terminationReason is an id of Select where we are adding options as innerHTML.

Also I have created a jsfiddle to suit your needs : http://jsfiddle.net/5pYkW/117/

Hope this helps. Please mark it as an answer if it resolves your query and let me know.

Thank you.


James LoghryJames Loghry

It would be a lot simpler / straight forward to generate your select options in Apex and then render those in Visualorce.  That being said, your result comes back in JSON format, so you could use the following example if you used jquery.  Note that this example is based off of a post on Stack Overflow: http://stackoverflow.com/questions/5918144/how-can-i-use-json-data-to-populate-the-options-of-a-select-box

Visualforce.remoting.Manager.invokeAction(             
    '{!$RemoteAction.AccountRemoter.getRemotePieData}'           
    ,pos1          
    ,function(result, event){                        
        if (event.status && result.constructor === Array){
            $.each(result, function(i, value) {
                $('#mySelect').append($('<option>').text(value).attr('value', value));
            })
        }
    }
);
I haven't tested this example, mind you, but should give you an idea on how you can accomplish your solution.  Also, there are other non jquery examples if you search the googles for "JSON to select options"