Search
 
SCRIPT & CODE EXAMPLE
 

HTML

multiple value selected in select2

var selectedValuesTest = ["WY", "AL", "NY"];
$(document).ready(function() {
  $("#e1").select2({
    multiple: true,
  });
  $('#e1').val(selectedValuesTest).trigger('change');
});
Comment

how to choose multiple option from select option

<!-- JUST ADD multiple ATTRIBUTE WITHIN select ATTRIBUTE -->
Cars:<br>
<select name="car" multiple>
    <option value="Volvo">Volvo</option>
    <option value="Thar">Thar</option>
    <option value="Audi">Audi</option>
</select>
Comment

select2 multiple html option

// took a day to figure out. Finally did it.
// select2 box with "multiple" "html" options with "remote ajax data" 
// created this as a php file to fetch remote data

<?php 

if (isset($_POST['db_leadsift_params'])) {

	$data = [
		['company_id'=>1, 'company_name'=>'a company', 'industry'=>'a industry'],
		['company_id'=>2, 'company_name'=>'b company', 'industry'=>'b industry'],
		['company_id'=>3, 'company_name'=>'c company', 'industry'=>'c industry'],
	];

	echo json_encode($data);
	exit();
}

?>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" integrity="sha512-nMNlpuaDPrqlEls3IX/Q56H36qvBASwb3ipuo3MxeWbsQB1881ox0cRv7UPTgBlriqoynt35KjEwgGUeUXIPnw==" crossorigin="anonymous" referrerpolicy="no-referrer" />

<select id="db_filter_company_name" data-param_type="company_name" style="width: 700px">
</select>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js" integrity="sha512-2ImtlRlf2VVmiGZsjm9bEyhjGW4dU7B6TNwh/hx/iSByxNENtj3WVE6o/9Lj4TJeVXPi4bnOIMXFIJJAeufa0A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script>
  
$(document).ready(function() {
  
 $('#db_filter_company_name').select2({
      closeOnSelect : false,
      multiple: true,
      allowHtml: true,
      allowClear: true,
      placeholder: 'Company Name',
      templateResult: select2formatCustom, //add html to options
      escapeMarkup: function(markup) { //to actually show custom html
        return markup;
      },
      templateSelection: function(data) { //shows value after selection inside select input box
        return data.text1;
      },
      ajax: {
          url: "",
          dataType: 'json',
          type: "POST",
          quietMillis: 2000,
          data: function (term) {
              return {
                  term: term,
                  db_leadsift_params: 1,
              };
          },
          processResults: function (data) {
              return {
                  results: $.map(data, function(obj) {
                      return { id: obj.company_id, text1: obj.company_name, text2: obj.industry };
                  })
              };
          }
      }
    });

    function select2formatCustom (state) {
        if (state.loading) return "Searching...";
        return '<div>'
        	+'<input type="checkbox" class="select2_my_checkbox" id="select2_my_checkbox'+state.id+'" data-id="'+state.id+'" />'
        	+state.text1+'</div> <p style="margin:0px">'+state.text2+'</p>';
    };

    //important part
    $(document).on("click",".select2-results__option",function() {
    	var id = $(this).find('.select2_my_checkbox').data('id');
    	if ($(this).hasClass('myactive')) {
    		console.log('uncheck')
            $("#db_filter_company_name option[value="+id+"]").prop("selected", false); //most important
			$("#db_filter_company_name").trigger("change");  //most important
            $("#select2_my_checkbox"+id).prop("checked", false);
            $(this).attr("aria-selected", false);
            $(this).removeClass('myactive');
        } else {
    		console.log('check')
        	$("#db_filter_company_name option[value="+id+"]").prop("selected", true); //most important
        	$("#db_filter_company_name").trigger("change"); //most important
        	$("#select2_my_checkbox"+id).prop("checked", true);
        	$(this).attr("aria-selected", true);
            $(this).addClass('myactive');
        }
    })

});

</script>
Comment

Select multiple

<script type="text/javascript">
    $(document).ready(function() {
        $('#example-post').multiselect({
            includeSelectAllOption: true,
            enableFiltering: true
        });
    });
</script>
<form class="form-horizontal" method="POST" action="post.php">
    <div class="form-group">
        <label class="col-sm-2 control-label">Multiselect</label>
        <div class="col-sm-10">
            <select id="example-post" name="multiselect[]" multiple="multiple">
                <option value="1">Option 1</option>
                <option value="2">Option 2</option>
                <option value="3">Option 3</option>
                <option value="4">Option 4</option>
                <option value="5">Option 5</option>
                <option value="6">Option 6</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">Text Input</label>
        <div class="col-sm-10">
            <input type="text" name="text" class="form-control" placeholder="Text Input" />
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <div class="checkbox">
                <label>
                    <input type="checkbox" name="checkbox"> Checkbox
                </label>
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <div class="radio">
                <label>
                    <input type="radio" name="radio"> Radio 1
                </label>
            </div>
            <div class="radio">
                <label>
                    <input type="radio" name="radio"> Radio 2
                </label>
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-default">Submit</button>
        </div>
    </div>
</form>
Comment

select 2 dropdown

$(".js-example-basic-multiple-limit").select2({
  maximumSelectionLength: 2
});
Comment

html select multiple

$(".chosen-select").chosen({
  no_results_text: "Oops, nothing found!"
})
Comment

select2 multiple selected values

in_array($value, $array)
Comment

PREVIOUS NEXT
Code Example
Html :: multiple countdown timer js 
Html :: balise a html 
Html :: creating a table in html 
Html :: html to text npm 
Html :: how to make the border invisible in html 
Html :: font awesome react color 
Html :: how open url in html in new window with custom size 
Html :: bootstrap btn colors 
Html :: html 2 classes 
Html :: HTML5 Video tag not working in Safari , iPhone and iPad 
Html :: timetable html 
Html :: html2pdf pagebreak option 
Html :: data-slide-to dynamic angular 
Html :: what is mongoose 
Html :: lorem ipsum full text 
Html :: a tag displays below img tag 
Html :: floating whatsapp button html 
Html :: add html to page chrome extension 
Html :: load image file from input in html 
Html :: link in html 
Html :: html input address 
Html :: html aside 
Html :: What is difference between React router and react router Dom? 
Html :: how to put the heading in th ecenter of th html page 
Html :: favicon specification 
Html :: offline p5.js html 
Html :: html table to pdf jquery 
Html :: u tag in html 
Html :: how to hyperlink in html 
Html :: mat-icon outline 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =