# Insert text into textarea at cursor

**URL:** <https://forum.radzen.com/t/insert-text-into-textarea-at-cursor/3354>\
**Category:** Blazor Server\
**Created:** [April 22, 2020, 12:27pm UTC](https://forum.radzen.com/t/insert-text-into-textarea-at-cursor/3354 "2020-04-22T12:27:58Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Otis](https://forum.radzen.com/letter_avatar_proxy/v4/letter/o/e47774/32.png) [@Otis](https://forum.radzen.com/u/Otis)\
**Post date:** [April 22, 2020, 12:27pm UTC](https://forum.radzen.com/t/insert-text-into-textarea-at-cursor/3354/1 "2020-04-22T12:27:58Z")

</div>

Hello  
My application currently on a button click adds a piece of text to the end of the text in a textarea box. I would Like for the user to place the cursor where they want to add the text and on the button press it does it. I am assuming that i have to use javascript to make it happen but i am unsure. can you please point me in the right direction?

---

<div class="post-metadata">

**Author:** ![enchev](https://forum.radzen.com/user_avatar/forum.radzen.com/enchev/32/16_2.png) [@enchev](https://forum.radzen.com/u/enchev)\
**Post date:** [April 22, 2020, 12:35pm UTC](https://forum.radzen.com/t/insert-text-into-textarea-at-cursor/3354/2 "2020-04-22T12:35:11Z")

</div>

This is what I've found:

> <https://stackoverflow.com/questions/11076975/how-to-insert-text-into-the-textarea-at-the-current-cursor-position>

  
Here is how to execute JavaScript from Blazor:  

> **[Call JavaScript functions from .NET methods in ASP.NET Core Blazor](https://docs.microsoft.com/en-us/aspnet/core/blazor/call-javascript-from-dotnet?view=aspnetcore-3.1)**
>
> Learn how to invoke JavaScript functions from .NET methods in Blazor apps.

---

<div class="post-metadata">

**Author:** ![Otis](https://forum.radzen.com/letter_avatar_proxy/v4/letter/o/e47774/32.png) [@Otis](https://forum.radzen.com/u/Otis)\
**Post date:** [April 24, 2020, 2:35pm UTC](https://forum.radzen.com/t/insert-text-into-textarea-at-cursor/3354/3 "2020-04-24T14:35:16Z")

</div>

Thanks for that, I am now able to call a javascript function from c# within blazor 🙂  
One problem though....I dont seem to be able to use the Radzen controls in order to do this. If i create standard button and textarea it works but it doesnt work with your controls.  
Is there a way i can use your controls and call javascript as well?

---

<div class="post-metadata">

**Author:** ![enchev](https://forum.radzen.com/user_avatar/forum.radzen.com/enchev/32/16_2.png) [@enchev](https://forum.radzen.com/u/enchev)\
**Post date:** [April 24, 2020, 2:52pm UTC](https://forum.radzen.com/t/insert-text-into-textarea-at-cursor/3354/4 "2020-04-24T14:52:34Z")

</div>

How do you call it from HTML button and textarea and how from Radzen components that is not working?

---

<div class="post-metadata">

**Author:** ![Otis](https://forum.radzen.com/letter_avatar_proxy/v4/letter/o/e47774/32.png) [@Otis](https://forum.radzen.com/u/Otis)\
**Post date:** [April 24, 2020, 3:01pm UTC](https://forum.radzen.com/t/insert-text-into-textarea-at-cursor/3354/5 "2020-04-24T15:01:20Z")

</div>

Copied below are the three lines from the .razor page in question

```
                        <button class="btn btn-warning" @onclick="isnert0Please">Does this fly?</button>
                        <textarea name="Textarea1" @ref="Textarea0" id="Textarea1" cols="30" rows="3">stuff in here</textarea>
                        <RadzenButton Disabled="false" Size="ButtonSize.Small" Text="Insert Account Number" Click="@AccountButtonClick">

```

As you can see from the first line, the @onlcick calls the misspelt function. If i try to put this within either of the radzen controls it throws a compile error either not knowing what @onciick is or if i put the "isnert0Please" within the click event of a radzen button it cannot see it.

---

<div class="post-metadata">

**Author:** ![enchev](https://forum.radzen.com/user_avatar/forum.radzen.com/enchev/32/16_2.png) [@enchev](https://forum.radzen.com/u/enchev)\
**Post date:** [April 24, 2020, 3:07pm UTC](https://forum.radzen.com/t/insert-text-into-textarea-at-cursor/3354/6 "2020-04-24T15:07:03Z")

</div>

> [@Otis](#):
>
> isnert0Please

Can you post the definition of this method?

---

<div class="post-metadata">

**Author:** ![Otis](https://forum.radzen.com/letter_avatar_proxy/v4/letter/o/e47774/32.png) [@Otis](https://forum.radzen.com/u/Otis)\
**Post date:** [April 24, 2020, 3:09pm UTC](https://forum.radzen.com/t/insert-text-into-textarea-at-cursor/3354/7 "2020-04-24T15:09:29Z")

</div>

```
public async Task isnert0Please()
{
    await _JSRuntime.InvokeVoidAsync("setElementById", Textarea0, " {0}");
}

```

And the javascript is even simpler (please ignore the hard coded areas and alerts for now)...........

```
    function setElementById(id, text) {
        var posn = document.getElementById("Textarea1").selectionStart;
        var txt = document.getElementById("Textarea1").innerHTML;
        alert(txt.substr(0, posn));
        alert(txt.substr(posn));
        var newtxt = txt.substr(0, posn) + text + txt.substr(posn);
        alert(newtxt);
        document.getElementById("Textarea1").innerText= newtxt;
    }
```

---

<div class="post-metadata">

**Author:** ![enchev](https://forum.radzen.com/user_avatar/forum.radzen.com/enchev/32/16_2.png) [@enchev](https://forum.radzen.com/u/enchev)\
**Post date:** [April 24, 2020, 3:21pm UTC](https://forum.radzen.com/t/insert-text-into-textarea-at-cursor/3354/8 "2020-04-24T15:21:55Z")

</div>

Here is the same code with RadzenTextArea and RadzenButton

```auto
@inject IJSRuntime JSRuntime

<RadzenTextArea id="Textarea0" />
<RadzenButton Size="ButtonSize.Small" Text="Insert Account Number" Click="@isnert0Please" />

@code {
    public async Task isnert0Please()
    {
        await JSRuntime.InvokeVoidAsync("setElementById", "Textarea0", " {0}");
    }
}
...
        function setElementById(id, text) {
            var posn = document.getElementById(id).selectionStart;
            var txt = document.getElementById(id).innerHTML;
            alert(txt.substr(0, posn));
            alert(txt.substr(posn));
            var newtxt = txt.substr(0, posn) + text + txt.substr(posn);
            alert(newtxt);
            document.getElementById(id).innerText = newtxt;
        }

```

 ![image](https://forum.radzen.com/uploads/default/original/2X/2/28f8aa843b57dd5b5c11d12effd1fa3e835fe8d9.png)

---

<div class="post-metadata">

**Author:** ![Otis](https://forum.radzen.com/letter_avatar_proxy/v4/letter/o/e47774/32.png) [@Otis](https://forum.radzen.com/u/Otis)\
**Post date:** [April 24, 2020, 3:29pm UTC](https://forum.radzen.com/t/insert-text-into-textarea-at-cursor/3354/9 "2020-04-24T15:29:26Z")

</div>

I am so so sorry, I can see now that the radzen textarea doesnt have an id by default whereas i typed it naturally 😊  
I think that it is time to step away from the keyboard.

---

<div class="post-metadata">

**Author:** ![enchev](https://forum.radzen.com/user_avatar/forum.radzen.com/enchev/32/16_2.png) [@enchev](https://forum.radzen.com/u/enchev)\
**Post date:** [April 24, 2020, 3:31pm UTC](https://forum.radzen.com/t/insert-text-into-textarea-at-cursor/3354/10 "2020-04-24T15:31:27Z")

</div>

No worries! Have a great weekend! 🙂

---

<div class="post-metadata">

**Author:** ![Otis](https://forum.radzen.com/letter_avatar_proxy/v4/letter/o/e47774/32.png) [@Otis](https://forum.radzen.com/u/Otis)\
**Post date:** [April 24, 2020, 3:42pm UTC](https://forum.radzen.com/t/insert-text-into-textarea-at-cursor/3354/11 "2020-04-24T15:42:11Z")

</div>

Before you go, were you able to read the text within the textarea? I am getting the position now which is good but i am not getting or setting the text within the textarea

---

<div class="post-metadata">

**Author:** ![enchev](https://forum.radzen.com/user_avatar/forum.radzen.com/enchev/32/16_2.png) [@enchev](https://forum.radzen.com/u/enchev)\
**Post date:** [April 24, 2020, 3:49pm UTC](https://forum.radzen.com/t/insert-text-into-textarea-at-cursor/3354/12 "2020-04-24T15:49:09Z")

</div>

Here is an example with the code I've posted in my first reply:

```auto
@page "/textarea"
@inject IJSRuntime JSRuntime

<RadzenTextArea id="Textarea0" />
<RadzenButton Size="ButtonSize.Small" Text="Insert Account Number" Click="@isnert0Please" />

@code {
    public async Task isnert0Please()
    {
        await JSRuntime.InvokeVoidAsync("insertAtCursor", "Textarea0", "Something to insert");
    }
}
...
      function insertAtCursor(id, myValue) {
            var myField = document.getElementById(id);
            //IE support
            if (document.selection) {
                myField.focus();
                sel = document.selection.createRange();
                sel.text = myValue;
            }
            //MOZILLA and others
            else if (myField.selectionStart || myField.selectionStart == '0') {
                var startPos = myField.selectionStart;
                var endPos = myField.selectionEnd;
                myField.value = myField.value.substring(0, startPos)
                    + myValue
                    + myField.value.substring(endPos, myField.value.length);
            } else {
                myField.value += myValue;
            }
        }

```

 ![image](https://forum.radzen.com/uploads/default/original/2X/c/c3894408d36dbd2751b80a3827818ff8c07f017d.png)

---

<div class="post-metadata">

**Author:** ![Otis](https://forum.radzen.com/letter_avatar_proxy/v4/letter/o/e47774/32.png) [@Otis](https://forum.radzen.com/u/Otis)\
**Post date:** [April 24, 2020, 4:03pm UTC](https://forum.radzen.com/t/insert-text-into-textarea-at-cursor/3354/13 "2020-04-24T16:03:58Z")

</div>

Instead of debugging I will (re)use that code instead. Thanks again for your help  
👍🏾
