New Metronic Docs!Added Integration Docs with Starter Kits Apps for Laravel, Laravel Livewire, Angular, Vue, Symfony, Blazor Server, Django & Flask & more
Browse Docs

resetting kt select


const statusEl = document.getElementById("statusFilter");
let statusSelect = null;
if (statusEl && typeof KTSelect !== "undefined") {
statusSelect = new KTSelect(statusEl);
}

statusEl.value = "";
$(statusEl).trigger("change");

doesnt seem to work while trying to reset the element

<select class="kt-select" multiple data-kt-select="true" data-kt-select-multiple="true"
id="statusFilter" data-kt-select-placeholder="-- Select Status --" data-kt-select-config='{
"optionsClass": "kt-scrollable overflow-auto max-h-[200px]"
}'
>

<option value="paid" data-kt-select-option='Paid'>Paid

</option>
<option value="not_paid" data-kt-select-option='Not Paid'>Not Paid</option>
<option value="active" data-kt-select-option='Active'>Active</option>
<option value="suspended" data-kt-select-option='Suspended'>Suspended</option>
</select>


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (3)


Hi

This appears to be a bug in the KT Select component where clearSelection() doesn't properly sync with the native select element. Your manual approach is the correct workaround. We will fix it in the select component.

Thanks



Hi

You need to use the KT Select component's API methods to properly reset it.
Here's the correct way to reset your KT Select component:

const statusEl = document.getElementById("statusFilter");
let statusSelect = null;

if (statusEl && typeof KTSelect !== "undefined") {
statusSelect = new KTSelect(statusEl);
}

// Method 1: Use clearSelection() method (RECOMMENDED)
if (statusSelect) {
statusSelect.clearSelection();
}

// Method 2: Use setSelectedOptions with empty array
if (statusSelect) {
statusSelect.setSelectedOptions([]);
}


Thanks



what worked for me ....


let statusEl = document.getElementById("statusFilter");
let statusSelect = null;
if (statusEl && typeof KTSelect !== "undefined") {
statusSelect = new KTSelect(statusEl);
}
if (statusEl && statusEl.instance) {
//Clear KTSelect internal state and UI
if (statusEl.instance._state) {
statusEl.instance._state._selectedOptions = [];
}
if (statusEl.instance._options) {
statusEl.instance._options.forEach(opt => opt.classList.remove("selected"));
}
if (statusEl.instance._displayElement) {
statusEl.instance._displayElement.setAttribute("data-selected", "0");
statusEl.instance._displayElement.innerHTML =
statusEl.instance._config.placeholder || "-- Select Status --";
}
Array.from(statusEl.options).forEach(opt => (opt.selected = false));


}
l
your method

clearSelection();

does clear the drop down but the selected options (although now unselected) still carry through the datatable payload.

thanks<pre></pre>


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(