Live Chat Icon For mobile
Live Chat Icon

How to copy text to the clipboard in Blazor?

Platform: Blazor| Category : JavaScript Interop, General

In Blazor, you can call a JavaScript method using JavaScript interop to copy the input element text to the clipboard.

In the following code, the text entered into the text box will be copied by clicking the button, and it will be displayed in the alert box.


@page "/"
@inject IJSRuntime JSRuntime

<div class="form-inline">
    <input id="form-control" type="text" value="Hello World" />
    <button type="button" class="btn btn-primary" @onclick="CopyTextToClipboard">Copy</button>

@code {
    private async Task CopyTextToClipboard()
        await JSRuntime.InvokeVoidAsync("copyClipboard");


. . .
.    .   .
        function copyClipboard() {
            /* Get the text field */
            var copyText = document.getElementById("form-control");
            /* Select the text field */
            /* Copy the text inside the text field */
            /* Alert the copied text */
            alert("Copied the text: " + copyText.value);

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.