Search
 
SCRIPT & CODE EXAMPLE
 

HTML

select2 html example

<html>
        <script src="https://code.jquery.com/jquery-2.2.4.js"></script>
        <link href="https://rawgit.com/select2/select2/master/dist/css/select2.min.css" rel="stylesheet" />
        <script src="https://rawgit.com/select2/select2/master/dist/js/select2.js"></script>

        <select id="example" style="width: 300px">
            <option></option>
            <option value="JAN">January</option>
            <option value="FEB">February</option>
            <option value="MAR">March</option>
            <option value="APR">April</option>
            <option value="MAY">May</option>
            <option value="JUN">June</option>
            <option value="JUL">July</option>
            <option value="AUG">August</option>
            <option value="SEP">September</option>
            <option value="OCT">October</option>
            <option value="NOV">November</option>
            <option value="DEC">December</option>
        </select>

        <script>
            window.onpageshow = function() {
                $('#example').select2({
                    allowClear: true,
                    placeholder: 'Select a month'
                });
            };
        </script>
</html>

Comment

show html in select2 option

var data = [{
   id: 0,
   text: 'enhancement',
   html: '<div style="color:green">enhancement</div>'
}, {
   id: 1,
   text: 'bug',
   html: '<div style="color:red">bug</div><div><small>This is some small text on a new line</small></div>'
}];

function template(data) {
    return data.html;
}

$("select").select2({
   data: data,
   templateResult: template,
   escapeMarkup: function(m) {
      return m;
   }
});
Comment

select2 select all option

$('.select2').select2({
  placeholder: 'Press CTRL+A for selecr or unselect all options'
});

$('.select2[multiple]').siblings('.select2-container').append('<span class="select-all"></span>');

$(document).on('click', '.select-all', function (e) {
  selectAllSelect2($(this).siblings('.selection').find('.select2-search__field'));
});

$(document).on("keyup", ".select2-search__field", function (e) {
  var eventObj = window.event ? event : e;
  if (eventObj.keyCode === 65 && eventObj.ctrlKey)
     selectAllSelect2($(this));
});
        
        
function selectAllSelect2(that) {

  var selectAll = true;
  var existUnselected = false;
  var item = $(that.parents("span[class*='select2-container']").siblings('select[multiple]'));

  item.find("option").each(function (k, v) {
      if (!$(v).prop('selected')) {
          existUnselected = true;
          return false;
      }
  });

  selectAll = existUnselected ? selectAll : !selectAll;

  item.find("option").prop('selected', selectAll);
  item.trigger('change');
}
Comment

PREVIOUS NEXT
Code Example
Html :: prefill input field html 
Html :: Simple example of using external file javascript in html 
Html :: bootrsrap 
Html :: how to change submit button size in html 
Html :: form submission with post 
Html :: how to import jquery modular 
Html :: meta description tag in html 
Html :: ng for 
Html :: metamask delete account 
Html :: how to add bg html 
Html :: convert element to html string 
Html :: html p tag 
Html :: dead link html 
Html :: Disabled href tag 
Html :: pug meta viewport 
Html :: make text bold with html 
Html :: html img max tam 
Html :: email full width images 
Html :: Bootstrap Image Grid (Responsive) 
Html :: etiqueta negritahtml 
Html :: website html css 
Html :: group checkbox html 
Html :: typo3 fluid format html 
Html :: collapsible accordion html+css only 
Html :: how to select the another page section in html 
Html :: html timed hide 
Html :: postman tutorial 
Html :: ins tag in html 
Html :: fs render html 
Html :: bootstrap nav tabs 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =