Resolved Blazor get html element using c# code

lukas9669

Member
Joined
Feb 7, 2022
Messages
9
Programming Experience
1-3
I wanna build a Tic Tac Toe Blazor game. To print the fields I am using html buttons and a foreach loop:
C#:
@if(MatchField != null)
    {
   

        @foreach(var field in MatchField)
        {
            <span>
            <button @onclick="() => PrintSymbolOnButton(field)" >

               @ElementOfMatchField.Symbol
            </button>
            </span>

        }
   
}

Matchfield is a list consisting the tictactoe fields.
The PrintSymbolOnButton method should print the players symbol (X(O) on the button.
The method looks like this:
C#:
public static void PrintSymbolOnButton(ElementOfMatchField field)
    {
   
        ElementOfMatchField.Symbol = "X";

    }

C#:
ElementOfMatchField()
is the class for the fields, it looks like this:
C#:
 public class ElementOfMatchField
    {
        public int Row { get; set; }

        public int Column { get; set; }

        public static string Symbol { get; set; }

        public Player ClickedBy { get; set; }

        public ElementOfMatchField(int row, int column)
        {
            this.Row = row;
            this.Column = column;
        }
    }

How can I write the
C#:
PrintSymbolOnButton()
Method to print the players symbol on this button which was clicked?
Is there a similar method to the JS function GetElementById?
 
This is addressing the title of your post: Blazor (both server side and web assembly) don't have direct access to the HTML elements. You'll have to invoke JavaScript and let the JavaScript access the HTML.
 

Latest posts

Back
Top Bottom