Need to make selected text as bold/italic/underline using javascript, and also save & retrieve the same using c#

DotnetDude picture DotnetDude · May 10, 2011 · Viewed 33k times · Source

I need to make selected text of textbox bold/italic/underline using javascript. For that i am using the following code.

<img src="~/images/Bold"  alt="Bold" onclick="changeFont('TextBox1','b');"  />
<img src="~/images/Italic" alt="Italic" onclick="changeFont('TextBox1','i');"  />
<img src="~/images/Underline" alt="Underline" onclick="changeFont('TextBox1','u');"  />

<script type="text/javascript" language="javascript">
    function changeFont(txt, change) {
        if (change == 'b') {
            if (document.getElementById(txt).style.fontWeight == 'bold')
                document.getElementById(txt).style.fontWeight = 'normal';
            else
                document.getElementById(txt).style.fontWeight = 'bold';
        }
        else if (change == 'i') {
            if (document.getElementById(txt).style.fontStyle == 'italic')
                document.getElementById(txt).style.fontStyle = 'normal';
            else
                document.getElementById(txt).style.fontStyle = 'italic';
        }
        else {
            if (document.getElementById(txt).style.textDecoration == 'underline')
                document.getElementById(txt).style.textDecoration = 'none';
            else
                document.getElementById(txt).style.textDecoration = 'underline';
        }
    }
</script>

But the issue here is, when i click on bold image its making the whole text into bold but not the selected text. It´s not working for the other two images either.

While saving the text of textbox I am unable to get the text including html tags even after trying with

document.getElementById('TextBox1').innerHTML;

I am able to get only the value of textbox.

Is there any way to save and retrieve the same using javascript or C#

Thanks in advance SC

Answer

Joakim picture Joakim · May 10, 2011

Here is a question that answers your problem about getting the highlighting text How to get selected text in textarea?

About making the selected text bold you would need to use html tags or something like bbcode and parse it to html when you print it on to a page.

EDIT: Here is a page that shows the jquery plugin "fieldselection" in action.

EDIT 2: Here is an example of how I would've done this: jsfiddle link

The HTML:

<input id="bold" type="button" value="B" />
<br />
<textarea id="editor"></textarea>

<div id="resultAsHtml"></div>
<br />
<div id="resultAsText"></div>

The javascript (jquery) code:

$(document).ready(function() {

    $("#editor").keyup(Update);

    function Update(){
        var text = $(this).val();
        var result = ParseToHtml(text);
        $("#resultAsHtml").html(result);
        $("#resultAsText").text(result);
    }

    $("#bold").click(function(){
        var range = $("#editor").getSelection();
        var textToReplaceWith = "[b]"+ range.text + "[/b]";
        $("#editor").replaceSelection(textToReplaceWith , true);

        var text = $("#editor").val();
        var result = ParseToHtml(text);
        $("#resultAsHtml").html(result);
        $("#resultAsText").text(result);
    });

    function ParseToHtml(text) {
        text = text.replace("[b]", "<b>");
        text = text.replace("[/b]", "</b>");
        text = text.replace("  ","&nbsp;");
        text = text.replace("\n","</br>");
        return text;
    }

    $("#bold").replaceSelection("[b]" + $("#editor").getSelection() + "[/b]", true);
});