Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
itchief committed Aug 22, 2021
1 parent fc635f4 commit 16647ce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions custom-select/custom-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CustomSelect {
this._elToggle.value = option.dataset['value'];
this._elToggle.dataset.index = option.dataset['index'];
this._elRoot.dispatchEvent(new CustomEvent('select.change'));
this._params.onSelected ? this._params.onSelected(option) : null;
this._params.onSelected ? this._params.onSelected(this, option) : null;
return option.dataset['value'];
}
_reset() {
Expand All @@ -51,7 +51,7 @@ class CustomSelect {
this._elToggle.value = '';
this._elToggle.dataset.index = -1;
this._elRoot.dispatchEvent(new CustomEvent('select.change'));
this._params.onSelected ? this._params.onSelected(null) : null;
this._params.onSelected ? this._params.onSelected(this, null) : null;
return '';
}
_changeValue(option) {
Expand Down
17 changes: 14 additions & 3 deletions custom-select/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,26 @@
['toyota', 'Toyota'],
['nissan', 'Nissan']
],
onSelected(option) {
onSelected(select, option) {
// выбранное значение
console.log(`Выбранное значение: ${select.value}`);
// индекс выбранной опции
console.log(`Индекс выбранной опции: ${select.selectedIndex}`);
// выбранный текст опции
const text = option ? option.textContent : '';
console.log(`Выбранное значение: ${text}`);
console.log(`Выбранный текст опции: ${text}`);
},
});
document.querySelector('.select').addEventListener('select.change', (e) => {
const btn = e.target.querySelector('.select__toggle');
// выбранное значение
console.log(`Выбранное значение: ${btn.value}`);
// индекс выбранной опции
console.log(`Индекс выбранной опции: ${btn.dataset.index}`);
// выбранный текст опции
const selected = e.target.querySelector('.select__option_selected');
const text = selected ? selected.textContent : '';
console.log(`Выбранное значение: ${text}`);
console.log(`Выбранный текст опции: ${text}`);
});
})
</script>
Expand Down

0 comments on commit 16647ce

Please sign in to comment.