2

How to populate drop down value in java script?

<script type="text/javascript">

var creatLimit = 5;
var fCount=0;   
function addFileElement() 
{

    if(fCount <creatLimit )
    {   
     /*var option = document.createElement("option");
        option.value = '0'; option.innerHTML = ' -- '; select.appendChild(option);
        option.value = '1'; option.innerHTML = 'item 1'; select.appendChild(option);
        option.value = '2'; option.innerHTML = 'item 2'; select.appendChild(option);*/
    var fObject = document.getElementById("agencySection"); 
    //var addButton = document.createElement(label);

    var addButton = document.createElement("select");
    var agency = document.getElementById("agencyLabelSection");
    var addButton2 = document.createElement("option");

    //<label for="firstname">First name:</label> 
    //<label for="agencyLabelSection">agency:</label> 
    //<input type="text" name="agencyLabelSection" id="agencyLabelSection" />

    addButton.type= "select";   
    addButton2.type= "option";
    //for (var fObject in addButton) {
    addButton.name="userRoleBeanList["+fCount+"]";  
    addButton.setAttribute("class", "normal");
    addButton.style.width= "250px"; 
    addButton.onkeydown = function(){
        blur();

    };  //}
    //document.write("<p> Agency:</p>");
    addButton2.name="userRoleBeanList["+fCount+"]"; 
    addButton2.setAttribute("class", "normal");
    addButton2.onkeydown = function(){
        blur();
    };  
    var o2 = document.createElement("br");
    var o3 = document.createElement("br");

    fObject.appendChild(addButton);
    fObject.appendChild(o2);
    fObject.appendChild(o3);
    agency.appendChild(addButton2);

    var fObject1 = document.getElementById("roleSection");  
    var addButton1 = document.createElement("select");

    var role = document.getElementById("roleLabelSection");
    var addButton3 = document.createElement("option");

    addButton1.type= "select";  
    addButton3.type= "option";

    addButton1.name="userRoleBeanList["+fCount+"]"; 
    addButton1.setAttribute("class", "normal");
    addButton1.style.width= "250px";    
    addButton1.onkeydown = function(){
        blur();
    };  

    var o4 = document.createElement("br");
    var o5 = document.createElement("br");

    fObject1.appendChild(addButton1);
    fObject1.appendChild(o4);
    fObject1.appendChild(o5);
    role.appendChild(addButton3);

    fCount++;
    }   
}
</script>
1
  • sapna if my answer is good for you feel free to accept it, if not please stat what more is needed Commented Jul 29, 2013 at 9:15

1 Answer 1

3

the same question was asked here and the answer is just

ddl.options[i] = theOption;

this code example show how to add variables to the drop down:

var ddl = document.getElementById( 'myDropdown' );
 var theOption = new Option;
 var x;
 var i;

 for(i = 0; i < 999; i++) {
 x = i + 1;
 theOption.text = x;
 theOption.value = x;
 ddl.options[i] = theOption;
 }

if you'll edit your question so we'll know what's the drop down list name and needed values are then i can help you more

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.