I have a .NET C#/Aspx web application in which users fill in fields and the resulting form is emailed out. I have a requirement to allow users to highlight text and then make that text either bold or coloured or both.
I have done a bit of searching on the net and found that javascript might be the best option. However, when the script inserts the html tags - it doesn't render within the textbox.I know this is a limitation of the asp textbox but is there anyway around this?
<script type="text/javascript">
function formatText(tag) {
var selectedText = document.selection.createRange().text;
if (selectedText != "") {
var newText = "<" + tag + ">" + selectedText + "</" + tag + ">";
document.selection.createRange().text = newText;
}
}
I have a .NET C#/Aspx web application in which users fill in fields and the resulting form is emailed out. I have a requirement to allow users to highlight text and then make that text either bold or coloured or both.
I have done a bit of searching on the net and found that javascript might be the best option. However, when the script inserts the html tags - it doesn't render within the textbox.I know this is a limitation of the asp textbox but is there anyway around this?
<script type="text/javascript">
function formatText(tag) {
var selectedText = document.selection.createRange().text;
if (selectedText != "") {
var newText = "<" + tag + ">" + selectedText + "</" + tag + ">";
document.selection.createRange().text = newText;
}
}
Share Improve this question asked Oct 4, 2012 at 19:01 user1721064user1721064 212 bronze badges 2
-
Do you need the text to be bold immediately? Or are you okay with adding literal text like
<b>
to the textbox, and allowing it to be passed to the email? – Ian Commented Oct 4, 2012 at 19:06 - I need it bold immeadiately (within the asp textbox) - I already have it inserting the bold tags before and after the highlighted text – user1721064 Commented Oct 5, 2012 at 8:17
3 Answers
Reset to default 7You'll need a rich text editor like ckeditor.
Instead reinventing the wheel, take a look at a rich text editor, like for example TinyMCE you can strip it down, so the user only have the options like bold or italic.
More info how to create a simple example can be found here:
Create a very simple TinyMCE wysiwyg Editor
Cannot be done in a Textbox.Go for any RichTextEditor. (CKEDITOR or CLEEDITOR)