Skip to content

Commit 156c2b9

Browse files
committed
kb(editor): create a text range
1 parent c1e31cd commit 156c2b9

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: Create a range, delete the selection and paste the desired content over the selection
3+
description: See how to create a range in RadEditor, delete programmatically the selection and replace it with your own content in RadEditor for ASP.NET AJAX
4+
type: how-to
5+
page_title: Alternative way to paste content with JavaScript in RadEditor
6+
slug: editor-create-range
7+
position:
8+
tags:
9+
ticketid: 1587670
10+
res_type: kb
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>Progress® Telerik® UI for ASP.NET AJAX</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
24+
## Description
25+
See how to replace the current document's selection with the desired HTML. The solution section shows how to create a text range in RadEditor, delete programmatically the selection and replace it with your own content in RadEditor for ASP.NET AJAX. This scenario is useful when you'd like not to use the built-in [pasteHtml method]({%slug editor/client-side-programming/methods/pastehtml%}).
26+
27+
## Solution
28+
For iframe content area mode here is an example of how to create a range, delete the selection and insert new content:
29+
30+
````ASP.NET
31+
<telerik:RadEditor ID="RadEditor1" runat="server" ContentAreaMode="iframe">
32+
<Content>
33+
<div>
34+
<p>My first paragraph!</p>
35+
<p>My second paragraph!</p>
36+
</div>
37+
<div>
38+
<p>My third paragraph!</p>
39+
<p>My fourth paragraph!</p>
40+
</div>
41+
</Content>
42+
</telerik:RadEditor>
43+
<input type="button" name="Insert Span" value="Insert Span" onclick="InsertSpan();return false;" />
44+
<script type="text/javascript">
45+
function InsertSpan() {
46+
var editor = $find("<%=RadEditor1.ClientID%>"); //get a reference to the editor
47+
var range = editor.getSelection().getRange(true); // https://docs.telerik.com/devtools/aspnet-ajax/controls/editor/client-side-programming/methods/selection/getrange
48+
range.deleteContents(); // https://developer.mozilla.org/en-US/docs/Web/API/Range/deleteContents
49+
var node = range.createContextualFragment('<span style="color:red;">test</span>'); // https://developer.mozilla.org/en-US/docs/Web/API/Range/createContextualFragment
50+
range.insertNode(node); // https://developer.mozilla.org/en-US/docs/Web/API/Range/insertNode
51+
}
52+
</script>
53+
````
54+
55+
For div content area mode, here is how to create a range, delete the selected content and replace it:
56+
57+
````ASP.NET
58+
<telerik:RadEditor ID="RadEditor1" runat="server" ContentAreaMode="div">
59+
<Content>
60+
<div>
61+
<p>My first paragraph!</p>
62+
<p>My second paragraph!</p>
63+
</div>
64+
<div>
65+
<p>My third paragraph!</p>
66+
<p>My fourth paragraph!</p>
67+
</div>
68+
</Content>
69+
</telerik:RadEditor>
70+
<input type="button" name="Insert Span" value="Insert Span" onclick="InsertSpan();return false;" />
71+
<script type="text/javascript">
72+
function InsertSpan() {
73+
var editor = $find("<%=RadEditor1.ClientID%>"); //get a reference to the editor
74+
var range = window.document.getSelection().getRangeAt(0); // https://developer.mozilla.org/en-US/docs/Web/API/Selection/getRangeAt
75+
range.deleteContents(); // https://developer.mozilla.org/en-US/docs/Web/API/Range/deleteContents
76+
var node = range.createContextualFragment('<span style="color:red;">test</span>'); // https://developer.mozilla.org/en-US/docs/Web/API/Range/createContextualFragment
77+
range.insertNode(node); // https://developer.mozilla.org/en-US/docs/Web/API/Range/insertNode
78+
}
79+
</script>
80+
````
81+
82+
You can find more information at:
83+
84+
* [JavaScript replace selection all browsers](https://stackoverflow.com/questions/5393922/javascript-replace-selection-all-browsers)
85+
* [MDM Selection API](https://developer.mozilla.org/en-US/docs/Web/API/Selection)
86+
87+

0 commit comments

Comments
 (0)