Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  
<script src="http://ivaynberg.github.io/select2/select2-3.4.5/select2.js?ts=2014-02-03T15%3A41%3A41-08%3A00"></script>  
  
      <link href="http://ivaynberg.github.io/select2/select2-3.4.5/select2.css?ts=2014-02-03T15%3A41%3A41-08%3A00" rel="stylesheet"/>
  
        <link href=" http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"/>
 
  
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  
 
 <select id="sel" style="width: 100%">
    <option value="1">Hello</option>
    <option value="2">Friends</option>
    <option value="3">Stackoverflow</option>
</select>
<br><br>
<button id="addok">Add OK <i class='fa fa-check'></i></button>
  <button id="removeok">Remove OK <i class=''></i></button>
  
 
  
</body>
</html>
 
$(document).ready(function () {
    $("#sel").select2({
          escapeMarkup: function(m) { return m; }
      });
    
    $("#addok").click(function() {
        actual_value = $("#sel").find(':selected').text();
        
        if (actual_value.indexOf(" <i class='fa fa-check'></i>") > -1){
            alert("asd");
            return;
        }
        
        newtext = actual_value + " <i class='fa fa-check'></i>";
        $("#sel").find(':selected').text(newtext).change();
    });
   
  $("#removeok").click(function() {
        
         actual_value= $("#sel").find(':selected').text(), indexOk =actual_value.indexOf(" <i class='fa fa-check'></i>");
        if (indexOk > -1){
              newtext =actual_value.substring(0, indexOk);
 $("#sel").find(':selected').text(newtext).change();
            return;
        }
    });
});
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers