0

I am trying to make a dynamic select dropdown, I tried writing the code in javascript and jquery both but can't append the options in the dropdown. When I tried seeing what is stored then the Html select code along with all the options is shown. What should I add or change in it so the options are stored in the correct position?

lines=["B","C","D","E","F","G"];
$(document).ready(function(){
  $("#select1").multiselect({
    placeholder: 'Features'
  });
});    

   // var selectFir = document.getElementById("select1");
      
   // for (var i = 0; i < firstLine.length; i++) {
         // var x = document.createElement("OPTION"), 
           //     txt = document.createTextNode(firstLine[i]);
          // x.appendChild(txt);
          //  x.setAttribute("value", firstLine[i]);
          //  selectFir.appendChild(x);
          //  selectFir.insertBefore(x, selectFir.lastChild);
   //  }console.log(selectFir);

firstLine = lines[0];
// Calls function to generate drop downs using options above
       for(var i=0; i< firstLine.length;i++)
       {
        //creates option tag
         jQuery('<option/>', {
               value: firstLine[i],
               html: firstLine[i]
               }).appendTo('#select1'); 
       }
       jQuery('#select1').multiselect('refresh');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/multiselect/2.2.9/js/multiselect.min.js"></script>
<select multiple id="select1">
 <option value="A">A</option>
</select>

4
  • Looks like you are looking forward to put code after this "// Calls function to generate drop downs using options above" in function on some click event, correct? Commented Nov 19, 2019 at 15:59
  • Yeah, the problem is I can see the whole select tag of html code when I display selectFir, it is not pushing the options in the options tag. Is there a jquery plugin that can populate dynamic dropdown? Commented Nov 19, 2019 at 17:19
  • Isn't solution code i provided answer your question? Commented Nov 21, 2019 at 14:28
  • Oh yeah, that helped, sorry I didn't reply, and thank you! Commented Nov 21, 2019 at 16:51

1 Answer 1

1

lines=["B","C","D","E","F","G"];
$(document).ready(function(){
  $("#select1").multiselect({
    placeholder: 'Features'
  });
});    

   // var selectFir = document.getElementById("select1");
      
   // for (var i = 0; i < firstLine.length; i++) {
         // var x = document.createElement("OPTION"), 
           //     txt = document.createTextNode(firstLine[i]);
          // x.appendChild(txt);
          //  x.setAttribute("value", firstLine[i]);
          //  selectFir.appendChild(x);
          //  selectFir.insertBefore(x, selectFir.lastChild);
   //  }console.log(selectFir);

firstLine = lines;
// Calls function to generate drop downs using options above
       for(var i=0; i< firstLine .length;i++)
       {
        //creates option tag
         jQuery('<option/>', {
               value: firstLine[i],
               html: firstLine[i]
               }).appendTo('#select1'); 
       }
       jQuery('#select1').multiselect('refresh');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/multiselect/2.2.9/js/multiselect.min.js"></script>
<select multiple id="select1">
 <option value="A">A</option>
</select>

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.