Click here to Skip to main content
15,895,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   <script type="text/javascript">
       var numOfDep = 0;
       var Myvalues = new Array();
       var selecID = "";
       function FillSelect(selectValue,SelectText)
       {
           var id = document.getElementById(selecID);
           var opt = document.createElement('option');
           opt.text = SelectText;
           id.appendChild(opt);
       }
       function AddDependent()
       {
           numOfDep++;
           var mainDiv = document.getElementById('MainDiv');
               var newDiv = document.createElement('div');
               newDiv.setAttribute('id', "ContainerDiv" + numOfDep);
               var newSpan = document.createElement('span');
               newSpan.innerHTML = "Insert Dependents No" + numOfDep + ":";
               var newTextBox = document.createElement('input');
               newTextBox.type = 'text';
               newTextBox.id = 'txt_Dep' + numOfDep;
               var NewSelect = document.createElement('input');
               NewSelect.id = 'select_Rela' + numOfDep;
               selecID = NewSelect.id;
               newDiv.appendChild(newSpan);
               newDiv.appendChild(newTextBox);
               newDiv.appendChild(NewSelect);
               mainDiv.appendChild(newDiv);
               FillSelect("Father");
               FillSelect("Mother");
               FillSelect("wife");
               
       }

   </script>
</head>
<body dir="ltr">
    <form id="form1" runat="server">
    <div id="MainDiv">
      <input id="Button1" type="button" value="Add Dependents" onclick="AddDependent();"/>
       
    </div>
    </form>
   
</body>
</html>


I need to fill select control that created in runtime
Posted
Comments
ArunRajendra 24-Mar-14 7:41am    
Should not fillselect take 2 params?
enhzflep 24-Mar-14 9:46am    
You seem to be neglecting the fact that (a) the FillSelect function takes two vars, yet you only provide it with one. the result is that SelectValue holds the only data you pass to that function, _yet_ it is unused in the function. That's not going to end well!

You also made a mistake with the line:
var NewSelect = document.createElement('input');

Can you see the problem here? (Hint: you should be creating a select element, not an input one)

Another improvement is to use the ability to create option elements directly. I.e, you can write something like: var newOpt = new Option(optText, optValue);

So, basically, you need to:

a) Make var NewSelect = document.createElement('input'); into var NewSelect = document.createElement('select');

b) Add a second param to your calls to FillSelect

c) Consider replacing the option creation code (1 line does more than you do in 2)

1 solution

Problems

  1. The Select Control creation should take select as a argument instead of input. So, change following code...
    JavaScript
    var NewSelect = document.createElement('input');
    to...
    JavaScript
    var NewSelect = document.createElement('select');
  2. FillSelect function takes two parameters. But it does not use selectValue parameter. So, change following code...
    JavaScript
    FillSelect("Father");
    FillSelect("Mother");
    FillSelect("wife");
    to...
    JavaScript
    FillSelect("Father", "Father");
    FillSelect("Mother", "Mother");
    FillSelect("wife", "wife");

    You can also delete the parameter selectValue from function and call the function only with SelectText parameter.

Demo

[Issue] html select control appear empty[^]
 
Share this answer
 
Comments
enhzflep 24-Mar-14 11:27am    
Exactly!
+5 for the answer and accompanying jsfiddle.
Thanks enhzflep. :)
enhzflep 25-Mar-14 3:25am    
Pleasure Tadit. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900