The New Way to Build with AI! Introducing ReUI — the developer platform for agentic UI with shadcn/ui
Browse ReUI Pro

KTSelect - Programmatically change values


Hi all.

I'm trying to find docs online as I need info on how to programmatically change values within a KTSelect item through javascript. I was able to trace getSelectedOptions and setSelectedOptions but somehow they are not working properly and not updated the UI. Is anyone aware of any relevant documentation or example?


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (1)

 
Deleted comment
 
Deleted comment

Hi

In the current implementation, setSelectedOptions() only updates KTSelect’s internal state. It does not update the native <select>, the trigger display, or the option styling in the dropdown. That’s a known limitation of the current API; we will fix it to also refresh the display and option.

This is a workaround for now. We will include the fix in this week's updates.

  1. Get the instance from the original <select>
<pre> const selectEl = document.querySelector('#your-select'); // your <select data-kt-select> const ktSelect = KTSelect.getInstance(selectEl) || new KTSelect(selectEl); </pre>
  1. Set value and refresh the UI
<pre> function setKTSelectValue(selectEl, value) { const ktSelect = KTSelect.getInstance(selectEl) || new KTSelect(selectEl); const option = selectEl.querySelector(`option[value="${value}"]`); if (!option) return; ktSelect.setSelectedOptions([option]); Array.from(selectEl.options).forEach(opt => { opt.selected = opt.value === value; }); selectEl.value = value; ktSelect.updateSelectedOptionDisplay(); } </pre>
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
buy metronic keenthemes mega bundle

ReUI

Open-source React components, blocks and templates built on shadcn/ui and Tailwind CSS by Keenthemes.