I am using select2 (multiselect)drop down in laravel .Here i am using world wide web option its click all option get select selected .But world wide web also get selected i want only option values et selected not World Wide web(Select All) option come in select box . How to fix it ?
<div >
<div >
<label for="service_continent">Service Continent</label>
<div >
<select id="service_continent" multiple="multiple" aria-label=".form-select-lg example" data-dropdown-css- name="service_continent[]" style="height:50px !important;">
<!-- <input type="checkbox" >Select All -->
<option value=''>Select Continent</option>
<option value="World Wide">World Wide</option>
@foreach($continents as $index => $continent)
<!-- <option value="{{ $index }}" @if(isset($company) && $company->service_continent == $index || old('service_continent')===$index) selected @endif>{{ $continent }}</option> -->
<option value="{{ $index }}" @if(isset($company) && in_array($index,$serviceContinent)|| (old('service_continent') && in_array($index, old('service_continent')))) selected @endif>{{ $continent }}</option>
@endforeach
</select>
@if ($errors->has('service_continent'))
<span >{{ $errors->first('service_continent') }}</span>
@endif
</div>
</div>
</div>
var service_continent = $('.service_continent').select2();
$('.service_continent').on("select2:select", function (e) {
var data = e.params.data.text;
if(data=='World Wide'){
$(".service_continent > option").prop("selected","selected");
$(".service_continent").trigger("change");
}
});
CodePudding user response:
This will help you!
$('.service_continent').select2();
$('.service_continent').on('change', function(event) {
event.preventDefault();
if ($(this).val() == 'all') {
// select all options except first one
$('.service_continent > option:not(:first)').prop('selected', true);
// unselect "Select All" option
$('.service_continent > option:first').prop('selected', false);
$('.service_continent').trigger('change');
}
});
