Skip to content

Latest commit

 

History

History
146 lines (117 loc) · 5.42 KB

copy-text-from-textbox-to-clipboard.md

File metadata and controls

146 lines (117 loc) · 5.42 KB
title description page_title slug tags res_type component
Copy Text from TextBox to Clipboard
Learn how to copy text from the {{ site.product }} TextBox to the Clipboard.
Copy Text from {{ site.product }} TextBox to Clipboard
copy-text-from-textbox-to-clipboard
textbox, clipboard, copy, button, suffix, telerik, core, mvc, aspnet, kendo
kb
textbox

Environment

Product {{ site.product }} TextBox
Product Version 2024.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()](https://docs.telerik.com/{{ site.platform }}/api/kendo.mvc.ui.fluent/textboxbuilder#suffixoptionssystemaction) configuration and define a template.
  2. Within the template, create a Button component and wire to its [Click](https://docs.telerik.com/{{ site.platform }}/api/kendo.mvc.ui.fluent/buttonbuilder#eventssystemaction) event.
  3. Within the event handler, use the Clipboard interface's writeText() method to copy the text from the TextBox.

{% if site.core %}

     @(Html.Kendo().TextBox()
        .Name("email")
        .Label(l => l.Content("Email Address").Floating(false))
        .Value("[email protected]")
        .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" })
    )
    <script id="buttonTemplate" type="text/html">
        <kendo-button name="copyBtn"
                      icon="copy"
                      on-click="onClick"
                      is-in-client-template="true">
        </kendo-button>
    </script>

    <kendo-textbox name="email" type="email">
        <suffix-options template-id="buttonTemplate" />
    </kendo-textbox>

{% else %}

     @(Html.Kendo().TextBox()
        .Name("email")
        .Label(l => l.Content("Email Address").Floating(false))
        .Value("[email protected]")
        .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" })
    )

{% endif %}

    <script>
        function onClick(e){
            var textBoxValue = $("#email").getKendoTextBox().value();
            navigator.clipboard.writeText(textBoxValue);
        }
    </script>    

{% if site.core %} To see an extended example of the aforementioned approach, refer to the following Telerik REPL example:

{% else %} To see an extended example of the aforementioned approach, refer to the REPL example on copying text from the TextBox within the Clipboard. {% endif %}

More {{ site.framework }} TextBox Resources

  • [{{ site.framework }} TextBox Documentation]({%slug htmlhelpers_overview_textbox %})

  • [{{ site.framework }} TextBox Demos](https://demos.telerik.com/{{ site.platform }}/textbox/index)

{% if site.core %}

{% else %}

See Also