Get 2024 Templates Mega Bundle!19 Bootstrap HTML, Vue & React Templates + 3 Vector Sets
Get for 99$

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


<div>
<div class="col-md-2">
<h4>metronic select2 test</h4>

<select class="form-select form-select-solid" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" id="select2_selectindex_1"
@onchange="@((ChangeEventArgs __e) => SelectedValue = __e?.Value.ToString())">
<option></option>
@foreach (var item in Select2Data)
{
<option value="@item.Id">@item.Name</option>
}
</select>

<span>Se&ccedil;im value: @(SelectedValue)</span>

<button onclick="@(() => SetSelectIndex("select2_selectindex_1"))">JS SelectedIndex Change Event</button>

</div>



@code {

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

void Select2Init()
{

Select2Data = new List<DataItem>();
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
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,
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
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  :(