Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to read select2 version 4 dropdown selected text?

I am trying to figure out the correct way to read the current selected text (not value) in select2 dropdown item. I don't see this listed on the documentation.

I can see there is a new DOM element that is the ID of the original select dropdown with the "-container" suffix and "select2-" prefix so not sure if this is the recommended to read this or select2 has another api call.

What is the correct way using jquery to read the current selected text?

like image 599
leora Avatar asked Jul 27 '15 01:07

leora


People also ask

How do I get selected text from Select 2?

Select2 version 2.4 let val, dom = '#your_selector_id'; val = $(dom+' option:selected'). text(); console. log('Initial text is '+val); $(document). on('change', () => { val = $(dom).

What Select2 dropdown?

By default, Select2 will attach the dropdown to the end of the body and will absolutely position it to appear above or below the selection container. Select2 will display the dropdown above the container if there is not enough space below the container, but there is enough space above it.

What does Select2 () do?

Select2 gives you a customizable select box with support for searching, tagging, remote data sets, infinite scrolling, and many other highly used options.

How does Select2 search work?

When users filter down the results by entering search terms into the search box, Select2 uses an internal "matcher" to match search terms to results. You may customize the way that Select2 matches search terms by specifying a callback for the matcher configuration option.


2 Answers

Just use the details from this answer: How to get Selected Text from select2 when using <input>

Like so:

$(function() { 
    // Initialise
    $('.example-basic-single').select2();
    $('.example-basic-multiple').select2();

    // Retrieve default selected value
    var defaultSelection = $('.example-basic-single').select2('data');
    $("#selectedS").text(defaultSelection[0].text);

    // Single select capture
    $('.example-basic-single').on("select2:select", function (e) { 
       var data = $(this).select2('data');
       $("#selectedS").text(data[0].text);
    });

    $('.example-basic-multiple').on("select2:select", function (e) { 
       var data = $(this).select2('data');
       var selectedText = $.map(data, function(selected, i) {
                              return selected.text;
                          }).join();
       $("#selectedM").text(selectedText);
    });
    $('.example-basic-multiple').on("select2:unselect", function (e) { 
       var data = $(this).select2('data');
       var selectedText = $.map(data, function(selected, i) {
                              return selected.text;
                          }).join();
       $("#selectedM").text(selectedText);
    });

});
.demo 
{ 
  margin: 10px;
}
.labelS, .labelM
{
  margin-top: 5px;
}
.selection
{ 
  margin-top: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://select2.github.io/dist/js/select2.full.js"></script>
<link href="https://select2.github.io/dist/css/select2.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
	<div class="row">
		<div class="col-xs-4">
			<div class="selection">Single select:</div>
			<select class="example-basic-single form-control">
				<option value="1">One</option>
				<option value="2">Two</option>  
				<option value="3">Three</option>
			</select>
			<div class="labelS">Selected:</div>
			<div id="selectedS"></div>
		</div>
		<div class="col-xs-4">  
			<div class="selection">Multiple Select:</div>
			<select class="example-basic-multiple form-control" multiple="multiple">
				<option value="1">One</option>
				<option value="2">Two</option>  
				<option value="3">Three</option>
				<option value="4">Four</option>
				<option value="5">Five</option>
			</select>
			<div class="labelM">Selected:</div>
			<div id="selectedM"></div>
		</div>
	</div>
</div>
like image 88
mccannf Avatar answered Sep 21 '22 05:09

mccannf


You can get select2 selected value using jquery option:selected and get .text() .

HTML

<select id="select2">
    <option value="1">Pizza</option>
    <option value="1">Hamburger</option>
    <option value="1">Salad</option>
    <option value="1">Gyro Wrap</option>
</select>

jQuery

$('#select2').select2();
$('#select2').on('change', function(){
    alert($("#select2 option:selected").text()) 
});

You can checkout http://jsfiddle.net/tu9h5zu8/1/

like image 39
Bhavin Solanki Avatar answered Sep 22 '22 05:09

Bhavin Solanki