Topic: Reset Material Select with Javascript/jQuery
givekidsclothes pro asked 7 years ago
<div class="col-md">
<div class="md-form">
<select id="pantSizeSelect" class="mdb-select"></select>
</div>
</div>
I initialize the select like this:
<script>
$(document).ready(function () {
$('.mdb-select').material_select();
$('#pantSizeSelect').append('<option value="0" disabled selected>Choose a Pant Size</option>');
}
</script>
I have tried all of the following and none work:
<script>
$('.select-dropdown li:eq(1)').trigger('click');
$('.select-dropdown li:eq(0)').trigger('click');
$('.select-dropdown').val('0');
</script>
Any suggestions?
rlambert pro answered 6 years ago
function resetSelect(id) { $(id).material_select('destroy'); $(id).val('0').change(); $(id).material_select(); }
// change handler for group list
function groupChange() {
var selected = $('#groupList').val(); console.log(selected);
resetSelect("#resourceList");
if (selected.length > 0) { selected.forEach(elem=> { var $div = $("<optgroup>", { label:"Resource Group: " + parseGroupName(elem) }); $("#resourceList").append($div); }); } }
function parseGroupName(id) { // "/subscriptions/2c923203-e491-4f5f-bf2c-5bc6210a1fc5/resourceGroups/enki" varidx = id.lastIndexOf("/"); return id.substring(idx+1); }
<div class="col-md-12 mb-4"> <select id="groupList" class="mdb-select" multiple onchange="groupChange();"> <option value="" disabled selected>Choose resource groups(s)</option> </select> </div>Thanks in advance for whatever help and advice you can give!
Adrian Sawicki free answered 7 years ago
Josip Tomašev priority answered 7 years ago
givekidsclothes pro answered 7 years ago
Josip Tomašev priority answered 7 years ago
richp pro commented 7 years ago
Thank you for sharing this. I was struggling with the same issue until I found your post.Vinicius Credendio free commented 5 years ago
Thx, worked like a charm!
givekidsclothes pro answered 7 years ago
<script>
$(document).ready(function () {
$('#pantSizeSelect').append('<option value="0" disabled selected>Choose a Pant Size</option>');
$('#pantSizeSelect').append('<option value="1">Large</option>');
$('.mdb-select').material_select();
}
</script>
Josip Tomašev priority answered 7 years ago
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Answered
- ForumUser: Pro
- Premium support: No
- Technology: General Bootstrap questions
- MDB Version: -
- Device: -
- Browser: -
- OS: -
- Provided sample code: No
- Provided link: Yes