Display Html in Select tag option

by abbaspanjwani

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/js/bootstrap-select.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/css/bootstrap-select.min.css">
<select class="selectpicker" id="countrycode" style='width:100px'>
  
  <option 
   data-content="<span class='label green'>&nbsp;&nbsp;&nbsp;&nbsp;India (+91)</span>" value="91"     
  ></option>
  
  <option data-content="<span class='label red'>
  &nbsp;&nbsp;&nbsp;&nbsp;USA (+1)</span>" value="1"></option>
  
  <option data-content="<span class='label yellow'>
  &nbsp;&nbsp;&nbsp;&nbsp;Algeria (+213)</span>" value="213"     
  ></option>
  
  <option data-content="<span class='label green'>
  &nbsp;&nbsp;&nbsp;&nbsp;Andorra (+376)</span>" 
  value="376" ></option>
  
</select>
Selected value: 
<span id='selectedCountry'></span>

CSS

.red {
    width: 18px;
    height: 18px;
    background: red;
    border: 2px solid red;
    -moz-border-radius: 50px;
    -webkit-border-radius: 50px;
    border-radius: 50px;
    display: inline-block;
    margin: 2px 10px 0 10px;
    color: black;
    content:' ';
}
.yellow {
    width: 18px;
    height: 18px;
    background: yellow;
    border: 2px solid yellow;
    -moz-border-radius: 50px;
    -webkit-border-radius: 50px;
    border-radius: 50px;
    display: inline-block;
    margin: 2px 10px 0 10px;
    color: black;
}
.green {
    width: 18px;
    height: 18px;
    background: green;
    border: 2px solid green;
    -moz-border-radius: 50px;
    -webkit-border-radius: 50px;
    border-radius: 50px;
    display: inline-block;
    margin: 2px 10px 0 10px;
    color: black;
}

JavaScript

$(document).ready(function(){
	$('#selectedCountry').html($('#countrycode').val());
	$('#countrycode').on('change',function(){
  	$('#selectedCountry').html($(this).val());
    
  });
});