HTML
xxxxxxxxxx
1
<select class="select">
2
<option value="1">One</option>
3
<option value="2">Two</option>
4
<option value="3">Three</option>
5
<option value="4" selected>Four</option>
6
<option value="5">Five</option>
7
<option value="6">Six</option>
8
<option value="7">Seven</option>
9
<option value="8">Eight</option>
10
</select>
11
12
<button class='btn btn-primary btn2'>
13
setValue('2')
14
</button>
15
<button class='btn btn-primary btn6'>
16
setValue('6')
17
</button>
CSS
1
1
JS
xxxxxxxxxx
1
const selectEl = document.querySelector('.select')
2
const selectInstance = mdb.Select.getInstance(selectEl)
3
const btn2 = document.querySelector('.btn2')
4
const btn6 = document.querySelector('.btn6')
5
6
// Get the selected option element and value
7
const selectedOption = document.querySelector('.select option[selected]');
8
9
if(selectedOption) {
10
// set default value using MDB Select method instead of 'selected' attr
11
selectInstance.setValue(selectedOption.value)
12
13
// Remove the 'selected' attribute
14
selectedOption.removeAttribute('selected');
15
}
16
17
18
btn2.addEventListener('click', () => selectInstance.setValue('2'))
19
btn6.addEventListener('click', () => selectInstance.setValue('6'))
20
Console errors: 0