Skip to content

Onclick event using variable in for loop #12235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
NaskoIT opened this issue Jul 16, 2019 · 3 comments
Closed

Onclick event using variable in for loop #12235

NaskoIT opened this issue Jul 16, 2019 · 3 comments
Labels
area-blazor Includes: Blazor, Razor Components question
Milestone

Comments

@NaskoIT
Copy link

NaskoIT commented Jul 16, 2019

Describe the bug

I am using Blazor and I want to generate 26 div elements which represent english alphabet.
When an element is clicked I want to invoke method with character in the div as parameter.

To Reproduce

Steps to reproduce the behavior:

  1. Using Blazor and .net core 3.0 preview
  2. Code
    <div class="d-flex justify-content-center flex-wrap w-75 mx-auto">
    @for (int i = 0; i < letters.Length; i++)
    {
    <div class="alphabet-letter text-dark" @onclick="@(() => Check(letters[i]))">@(letters[i].ToString().ToUpper())</div>
    }
    </div>

@code{
char[] letters = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
}
4. The error when I click on some div is IndexOutOfRangeException because the variable "i" is 26 when the element is clicked.

More details

This error is predictable because when I click some div, for loop has finished yet and the value of variable i is 26 in this moment.

I can fix my problem with this solution:
<div class="alphabet-letter text-dark" @onclick="@(() => Check('a'))">A</div>
<div class="alphabet-letter text-dark" @onclick="@(() => Check('b'))">B</div>
<div class="alphabet-letter text-dark" @onclick="@(() => Check('c'))">C</div>
<div class="alphabet-letter text-dark" @onclick="@(() => Check('d'))">D</div>
And continue with all letters to z.
But I want a bit more smart solution.

In the onclick event I need to use the value of variable "i" in the moment when div was generated.

Do you know some better solution for my problem?

@mkArtakMSFT mkArtakMSFT added area-blazor Includes: Blazor, Razor Components question labels Jul 16, 2019
@mkArtakMSFT mkArtakMSFT added this to the Discussions milestone Jul 16, 2019
@mkArtakMSFT
Copy link
Contributor

Thanks for contacting us, @NaskoVasilev.
@enetstudio 's answer is a good way you can do that.

@NaskoIT
Copy link
Author

NaskoIT commented Jul 16, 2019

Thanks a lot. The solution help me solve my problem.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components question
Projects
None yet
Development

No branches or pull requests

3 participants