JavaScript: Create Dynamic HTML Select Box element and there options

If want creating dynamic HTML Select Box use of JavaScript so its very easy to use you can just use this function.

function dynamicSelectBox(idForUse,options_value){
//options_value (provided format should be string with comm “one,two,three”)
 var choices = options_value.split(“,”);
   var ele = document.createElement(‘select’);
   ele.className = “form-control”;
   ele.options[0] = new Option(“Please select”,”0″);
for(var i=1;i<choices.length+1;i++){
     ele.options[i] = new Option(choices[i-1],choices[i-1]);
   }
// provide id where you want append this created element
document.getElementById(idForUse).appendChild(ele);
}

Explanation:
//your first parameter here div id
<div id=”demoId”></div>

//your second parameter here Options_string
var options = [“one”,”two”,”three”];
var Options_string = options.split(“,”);

//how to pass parameter in function
dynamicSelectBox(“demoId“,Options_string );

News Reporter
IT Software and web development, Core PHP, JavaScript, JQuery, HTML, CSS, CakePHP, Angularjs, Codeigniter etc.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: