Super Sale Limited Time 50% OFF for All-Access Plans
Save 50% Now

Blazor - Select2, I cannot change the index of select2 in javascript

Hi, I cannot change the index of select2 in javascript? An example was added as a video. How do I fix this situation?

https://drive.google.com/file/d/1fYAsiyZIx7ZusFrYkF0fOzfm8Im5in6K/view?usp=sharing

metronic select2 test

Seçim value: @(SelectedValue)
@code {

private string SelectedValue { get; set; } = "2";
private List Select2Data { get; set; } = new();

void Select2Init()
{

 Select2Data = new List();
 Select2Data.Add(new DataItem() { Id = "1", Name = "Liste 1" });
 Select2Data.Add(new DataItem() { Id = "2", Name = "Liste 2" });
 Select2Data.Add(new DataItem() { Id = "3", Name = "Liste 3" });
 Select2Data.Add(new DataItem() { Id = "4", Name = "Liste 4" });
 Select2Data.Add(new DataItem() { Id = "5", Name = "Liste 5" });
 Select2Data.Add(new DataItem() { Id = "6", Name = "Liste 6" });
}

async void SetSelectIndex(string elementId)
{
 await js.InvokeVoidAsync("elementSelectedIndexFunction.selectElementById", $"{elementId}", 3);
}

}
*** JS Code ***

window.elementSelectedIndexFunction =
{
 selectElementById: function (elementById, indexValue) {
 var element = document.getElementById(elementById);

 if (element != null) {
 element.selectedIndex = indexValue;
 }
 },
}
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 (3)


Hi,
Since there is no change in the interface, the user cannot notice this situation.



Hi Mustafa,

Could you please provide more details about your issue? This will allow me to assist you more effectively.

Regards,
Lauris Stepanovs,
Keenthemes Support Team



Hi,

Your code should change Select2Data state in your component, please make sure that the options values match the value you are trying to select.

Are you getting any errors when you try to change the selected item?

Regards,
Lauris Stepanovs,
Keenthemes Support Team


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