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

KTSelect.getOrCreateInstance is not a function


I’m running into a “KTSelect.getOrCreateInstance is not a function” error when I try to initialize KTSelect. Here’s my code:
<select id="districtSelect"
class="kt-select"
data-kt-select="true"
data-kt-select-placeholder="Seçiniz"
data-kt-select-multiple="true"
data-kt-select-config='{
"optionsClass": "kt-scrollable overflow-auto max-h-[250px]"
}'>
</select>
const districtEl = document.querySelector("#districtSelect");
var districtSelect = KTSelect.getOrCreateInstance(districtEl, {
placeholder: 'Åžehir seçiniz',
enableSearch: true,
searchPlaceholder: 'Ara...'
});


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 (1)


Hi

KTSelect attaches its instance to the DOM element as `element.instance`.

Best practice:
- Before creating a new instance, check if `element.instance` exists and is a `KTSelect` instance.
- If not, create a new one.

Example:

const districtEl = document.querySelector("#districtSelect");
let districtSelect = districtEl.instance;
if (!districtSelect) {
districtSelect = new KTSelect(districtEl, {
placeholder: "&Aring;&#158;ehir se&ccedil;iniz",
enableSearch: true,
searchPlaceholder: "Ara..."
});
}


To avoids double-initialize and the error, You can create your helper:

function getOrCreateKTSelect(element, config) {
if (!element.instance) {
element.instance = new KTSelect(element, config);
}
return element.instance;
}


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  :(