Function to Create Select Element

Creating select element using function

by syamsoul

HTML

<div id="selectElement">
  
  </div>

JavaScript

var selectEl_string = replTypeSelect([
  {val:'my', text:'Malaysia'},
  {val:'uk', text:'United Kingdom'},
  {val:'us', text:'United State'},
  {val:'id', text:'Indonesia'},
  {val:'jp', text:'Japan'},
], 'my');

$('#selectElement').html(selectEl_string);

function replTypeSelect(replTypeOptions, replType){
    var sel = $('<select/>');
    
    $.each(replTypeOptions, function(i, item) {
        var option = $("<option/>");
        option.attr('value', item.val);
        option.text(item.text);

        if(item.val == replType){
            option.prop('selected',true);
        }
        option.appendTo(sel);
    });

    return sel[0].outerHTML;
}