javascript - .net c# asp.net make part of Textbox text bold - Stack Overflow

I have a .NET C#Aspx web application in which users fill in fields and the resulting form is emailed o

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
Add a ment  | 

3 Answers 3

Reset to default 7

You'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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744282308a4566629.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信