New to Telerik UI for ASP.NET CoreStart a free 30-day trial

Copy Text from TextBox to Clipboard

Environment

ProductTelerik UI for ASP.NET Core TextBox
Product Version2024.1.130

Description

How can I copy text from the TextBox component directly within the Clipboard?

Solution

To achieve the desired result:

  1. Utilize the TextBox's SuffixOptions() configuration and define a template.
  2. Within the template, create a Button component and wire to its Click event.
  3. Within the event handler, use the Clipboard interface's writeText() method to copy the text from the TextBox.
Razor
     @(Html.Kendo().TextBox()
        .Name("email")
        .Label(l => l.Content("Email Address").Floating(false))
        .Value("test@mail.com")
        .SuffixOptions(suffix => suffix
             .Template(Html.Kendo().Template()
                .AddComponent(component => component
                    .Button()
                    .Name("copyBtn")
                    .Icon("copy")
                    .Events(events => events.Click("onClick"))
                )
             )
        
        )
        .HtmlAttributes(new { style = "width: 100%;", type = "email" })
    )

To see an extended example of the aforementioned approach, refer to the following Telerik REPL example:

More ASP.NET Core TextBox Resources

See Also