I am using a theme for my project. I am facing an issues related to append options in a select using jQuery because of theme's javascript.
When I append an option to select it doesn't show up although it is added. I found out that this is because a button and div are added above the select after page load. I am pasting 2 (different codes) for reference:
Actual Code
The code I did in laravel blade template
<div class="form-group">
<select class="selectpicker search-fields" id="ct" name="ct" data-live-search="true" data-live-search-placeholder="Search value">
<option value="">Contract Types</option>
@foreach (\App\ContractType::all() as $ct)
<option value="{{ $ct->id }}">{{ $ct->type }}</option>
@endforeach
</select>
</div>
Code inspected in browser
The code when I inspect element the select in browser
<div class="form-group">
<div class="btn-group bootstrap-select search-fields open">
<button type="button" class="btn dropdown-toggle bs-placeholder btn-default" data-toggle="dropdown" role="button" data-id="ct" title="Contract Types" aria-expanded="true">
<span class="filter-option pull-left">Contract Types</span>
<span class="bs-caret">
<span class="caret"></span>
</span>
</button>
<div class="dropdown-menu open" role="combobox" style="max-height: 590px; overflow: hidden; min-height: 158px;">
<div class="bs-searchbox">
<input type="text" class="form-control" autocomplete="off" placeholder="Search value" role="textbox" aria-label="Search">
</div>
<ul class="dropdown-menu inner" role="listbox" aria-expanded="true" style="max-height: 543px; overflow-y: auto; min-height: 111px;">
<li data-original-index="0" class="selected active">
<a tabindex="0" class="" data-tokens="null" role="option" aria-disabled="false" aria-selected="true">
<span class="text">Contract Types</span>
<span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
<li data-original-index="1">
<a tabindex="0" class="" data-tokens="null" role="option" aria-disabled="false" aria-selected="false">
<span class="text">Rent</span>
<span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
<li data-original-index="2">
<a tabindex="0" class="" data-tokens="null" role="option" aria-disabled="false" aria-selected="false">
<span class="text">Buy</span>
<span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
<li data-original-index="3">
<a tabindex="0" class="" data-tokens="null" role="option" aria-disabled="false" aria-selected="false">
<span class="text">Commercial Rent</span>
<span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
<li data-original-index="4">
<a tabindex="0" class="" data-tokens="null" role="option" aria-disabled="false" aria-selected="false">
<span class="text">Commercial Buy</span>
<span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
</ul>
</div>
<select class="selectpicker search-fields" id="ct" name="ct" data-live-search="true" data-live-search-placeholder="Search value" tabindex="-98">
<option value="">Contract Types</option>
<option value="1">Rent</option>
<option value="2">Buy</option>
<option value="3">Commercial Rent</option>
<option value="4">Commercial Buy</option>
</select>
</div>
</div>
Because of this, when I try to empty the select and append new options to it using jQuery with another select's on-change event:
$('#c').on('change', function(e){
var id = e.target.value;
$('#ct').empty();
$('#ct').append('<option value="">Contract Types</option>');
$('#ct').append('<option value="1">Rent</option>');
$('#ct').append('<option value="2">Buy</option>');
if (id == 1) {
$('#ct').append('<option value="3">Commercial Rent</option>');
$('#ct').append('<option value="4">Commercial Buy</option>');
}
});
it performs it in select but doesn't show up because of shadowing button & div. I've checked the theme's JS page from where it is doing this but I can't understand the whole function.