Blazor

Where should I access the DOM elements?

Where should I access the DOM elements?

You have to access the DOM elements in OnAfterRender or OnAfterRenderAsync of the Blazor component lifecycle.

[JS Helper]

window.methods = {    
    accessDOM: function () {
        // access DOM here
        $(".btn").text();
    }
}

[Razor]

@inject IJSRuntime JSRuntime

<button type="button" class="btn btn-primary">Submit</button>
@code{
    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        JSRuntime.InvokeAsync<object>("methods.accessDOM");
    }
}

Have a look at the Blazor component lifecycle for more information:

https://docs.microsoft.com/en-in/aspnet/core/blazor/components?view=aspnetcore-3.0#lifecycle-methods

Sumitra Kumari Shaw

An Engineering graduate who Enjoys building things that live on the internet.