0

I'm trying to populate a dropdown values using jquery.

 <select class="browser-default" id="autopopulate" name="autopopulate">

  </select>

I'm populating the names using this

var names = ['a','b','c']

        for(let i=0;i<=names.length - 1;i++){
              let d = names[i]; 
              
              $("#autopopulate")
                  .append($("<option></option>")
                  .attr("value" ,d)
                  .text(d));
                }

How to append values to existing names

 var values = ['ab','bc','df']

For name 'a' - value is 'ab'.

  • 1
    Does this answer your question? [jQuery: Best practice to populate drop down?](https://stackoverflow.com/questions/815103/jquery-best-practice-to-populate-drop-down) – kvn Jul 13 '20 at 17:08
  • How do you know which value goes with which name? Position in array? Then just set the value to `values[i]`. – Heretic Monkey Jul 13 '20 at 17:11
  • Same as `names` - `'a' - 'ab'` –  Jul 13 '20 at 17:12
  • "Same as `names`" doesn't answer the question, but I'm guessing my assumption of position in array was correct. So `.attr("value", values[i])`. – Heretic Monkey Jul 13 '20 at 17:20
  • Sorry, Yes position in array. –  Jul 13 '20 at 17:21

0 Answers0