javascript - Adding option values to a select box by comma spaced values -


how can code below modified, such able add single , comma separated values select box?

example 1: orange

[select box]

orange 

example2: red,blue,green,yellow

[select box]

red blue green yellow 

here html

<!doctype html>  <html>  <head>  <script type="text/javascript"> function add() {  var select = document.getelementbyid('list')   var option = document.getelementbyid('reference').value  select.options[select.options.length] = new option(option, option)  } </script>  </head>  <body> <input type="button" value="add" onclick="add()"> <input type="text" id="reference"> <br><br> <select style="width: 200px;" id="list"><option> </body>  </html> 

simply split value comma, , loop through each of strings, appending option go:

function add() {     var select = document.getelementbyid('list')     var options = document.getelementbyid('reference').value.split(',');     for(i=0; i<options.length; i++){         select.options[select.options.length] = new option(options[i], options[i])     } } 

jsfiddle


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -