@page "/"
@using Microsoft.JSInterop
@inject IJSRuntime JS
@functions{
ElementReference TextAreaRef;
int lineNumber = 0;
string TextAreaText = "Keep clicking the button and watch the vertical scroll move down.\n";
void ScrollToEnd() {
lineNumber++;
TextAreaText += $"Line number {lineNumber}\n";
JS.InvokeVoidAsync("scrollToEnd", new object[] {TextAreaRef});
}
}
<p>
Demostrates how to call a JavaScript function from C# that scrolls a textarea to the end.
</p>
<button class="btn btn-primary m-2" @onclick="ScrollToEnd">Add Line</button>
<br/>
<textarea @ref=TextAreaRef value="@TextAreaText" class="form-control" rows="5"></textarea>