Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

How To Generate Rich Text Box using JavaScript

, 1 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
If you are developing any site and in you site some section need user input in rich text format then you can't use text area because it's limitation is there. In market there are lots of control available freely and paid as well. But have you think ever how to develop our own text editor, so we can

Screenshots for Editor

Rich Text entered into RTB

HTML in RTB

Introduction

If you are developing any site and in you site some section need user input in rich text format then you can't use text area because it's limitation is there. In market there are lots of control available freely and paid as well. But have you think ever how to develop our own text editor, so we can customize as per our choice, need. There is no DLL limitation.

Today I will show you how to develop your RichText Box using JavaScript and HTML with CSS.

Method

I will not develop complete RichText box here, but some basic of the RichText Box and left remaining things for your exercise.

Add one HTML page to your project. Then in head section add below style for DropDown list. Add this line too in head section.

<style type="text/css" media="screen,print"> 
.select
{
	background-color:#CCCCCC;
	border-width:1px;
	font-family:Calibri,Verdana, Tahoma;
	margin:0;
	font-size:10px;
}
</style>

This is the file which encode and decode HTML tag to toggle between Design mode and HTML mode.

Add below code also inside head tag

<script type="text/javascript">
function Set()
{
    getIFrameDocument("textEditor").designMode = "On";
    getIFrameDocument("textEditor").focus();
}

function getIFrameDocument(editor)
{
    // If Fire fox Mozilla
    if (document.getElementById(editor).contentDocument)
    {
        return document.getElementById(editor).contentDocument;
    }
    // If Internet Explorer
    else
    {
        // IE
        return document.frames[editor].document;
    }
}
</script>

Set function to set the design mode on and focus as well. This function will get call once page loaded successfully. getIFrameDocument function will be used to check it's an IE or Firefox.

This is the another JavaScript code snippets which will be used to edit the document text as per our command. Add it to the head tag

<script type="text/javascript">
             var isDesignMode = true;
function fontEdit(command, y)
{
    switch (command)
    {
    case 'bold':
        if (document.getElementById('bold').src.toString().indexOf("over") != -1)
        {
            document.getElementById('bold').src = "images/bold.gif";
        }
        else
        {
            document.getElementById('bold').src = "images/bold.over.gif";
        }
        break;
    case 'italic':
        if (document.getElementById('italic').src.toString().indexOf('over') != -1)
            document.getElementById('italic').src = 'images/italic.gif';
        else
            document.getElementById('italic').src = 'images/italic.over.gif';
        break;
    case 'underline':
        if (document.getElementById('underline').src.toString().indexOf('over') != -1)
            document.getElementById('underline').src = 'images/underline.gif';
        else
            document.getElementById('underline').src = 'images/underline.over.gif';
        break;
    case 'justifyleft':
        if (document.getElementById('justifyleft').src.toString().indexOf('over') != -1)
            document.getElementById('justifyleft').src = 'images/justifyleft.gif';
        else
            document.getElementById('justifyleft').src = 'images/justifyleft.over.gif';
        break;
    case 'justifycenter':
        if (document.getElementById('justifycenter').src.toString().indexOf('over') != -1)
            document.getElementById('justifycenter').src = 'images/justifycenter.gif';
        else
            document.getElementById('justifycenter').src = 'images/justifycenter.over.gif';
        break;
    case 'justifyright':
        if (document.getElementById('justifyright').src.toString().indexOf('over') != -1)
            document.getElementById('justifyright').src = 'images/justifyright.gif';
        else
            document.getElementById('justifyright').src = 'images/justifyright.over.gif';
        break;
    case 'insertorderedlist':
        if (document.getElementById('insertorderedlist').src.toString().indexOf('over') != -1)
            document.getElementById('insertorderedlist').src = 'images/numberedlist.gif';
        else
            document.getElementById('insertorderedlist').src = 'images/numberedlist.over.gif';
        break;
    case 'insertunorderedlist':
        if (document.getElementById('insertunorderedlist').src.toString().indexOf('over') != -1)
            document.getElementById('insertunorderedlist').src = 'images/bulletedlist.gif';
        else
            document.getElementById('insertunorderedlist').src = 'images/bulletedlist.over.gif';
        break;
    case 'outdent':
        if (document.getElementById('outdent').src.toString().indexOf('over') != -1)
            document.getElementById('outdent').src = 'images/outdent.gif';
        else
            document.getElementById('outdent').src = 'images/outdent.over.gif';
        break;
    case 'indent':
        if (document.getElementById('indent').src.toString().indexOf('over') != -1)
            document.getElementById('indent').src = 'images/indent.gif';
        else
            document.getElementById('indent').src = 'images/indent.over.gif';
        break;
    case 'default':
        break;
    }
    getIFrameDocument("textEditor").execCommand(command, "", y);
    textEditor.focus();
}

function ChangeMode()
{
    if (isDesignMode == true)
    {
        isDesignMode = false;
        document.getElementById('mode').src = 'images/mode.html.gif';
        document.getElementById('mode').title = 'HTML View';
        Encoder.EncodeType = "entity";
        var encoded = Encoder.htmlEncode(getIFrameDocument("textEditor").body.innerHTML);
        getIFrameDocument("textEditor").body.innerHTML = encoded;
    }
    else
    {
        isDesignMode = true;
        document.getElementById('mode').src = 'images/mode.design.gif';
        document.getElementById('mode').title = 'Design View';
        var decoded = Encoder.htmlDecode(getIFrameDocument("textEditor").body.innerHTML);
        getIFrameDocument("textEditor").body.innerHTML = decoded;
    }
}
</script>

Now JavaScript is over and we will add HTML code to the page. Before that we will code Set function on page load event like this

<body onload="Set()"> 

And code is as below.

<div style="background-color: #CCCCCC; width: 500px;">
				<a onclick="javascript:fontEdit('bold')">
				<img id="bold" src="images/bold.gif" alt="Bold" title="Bold" style="height: 20px; width: 21px; border: none;" />
				</a><a onclick="javascript:fontEdit('italic')">
				<img id="italic" src="images/italic.gif" alt="" title="Italic" style="height: 20px; width: 21px; border: none;" />
				</a><a onclick="javascript:fontEdit('underline')">
				<img id="underline" src="images/underline.gif" alt="" title="Underline" style="height: 20px; width: 21px; border: none;" />
				</a>
				<img id="sep1" src="images/sep.gif" alt="" style="height: 16px; width: 1px; border: none;" />
				<a onclick="javascript:fontEdit('justifyleft')">
				<img id="justifyleft" src="images/justifyleft.gif" alt="" title="Justify Left" style="height: 20px; width: 21px; border: none;" />
				</a><a onclick="javascript:fontEdit('justifycenter')">
				<img id="justifycenter" src="images/justifycenter.gif" alt="" title="Justify Center" style="height: 20px; width: 21px; border: none;" />
				</a><a onclick="javascript:fontEdit('justifyright')">
				<img id="justifyright" src="images/justifyright.gif" alt="" title="Justify Right" style="height: 20px; width: 21px; border: none;" />
				</a>
				<img id="sep2" src="images/sep.gif" alt="" style="height: 16px; width: 1px; border: none;" />
				<a onclick="javascript:fontEdit('insertorderedlist')">
				<img id="insertorderedlist" src="images/numberedlist.gif" alt="" title="Order List" style="height: 20px; width: 21px; border: none;" />
				</a><a onclick="javascript:fontEdit('insertunorderedlist')">
				<img id="insertunorderedlist" src="images/bulletedlist.gif" alt="" title="Bullets List" style="height: 20px; width: 21px; border: none;" />
				</a><a onclick="javascript:fontEdit('outdent')">
				<img id="outdent" src="images/outdent.gif" alt="" title="Outdent" style="height: 20px; width: 21px; border: none;" />
				</a><a onclick="javascript:fontEdit('indent')">
				<img id="indent" src="images/indent.gif" alt="" title="Indent" style="height: 20px; width: 21px; border: none;" />
				</a><a onclick="ChangeMode()">
				<img id="mode" src="images/mode.design.gif" alt="" title="Design View" style="height: 20px; width: 21px; border: none;" />
				</a></div>
<div style="background-color: #CCCCCC; width: 497px; padding-left: 3px;">
				<select id="fonts" class="select" onchange="fontEdit('fontname',this[this.selectedIndex].value)">
				<option value="Arial">Font</option>
				<option value="Arial">Arial</option>
				<option value="Courier New">Courier New</option>
				<option value="Monotype Corsiva">Monotype</option>
				<option value="Tahoma">Tahoma</option>
				<option value="Times">Times</option>
				</select>
				<select id="size" class="select" onchange="fontEdit('fontsize',this[this.selectedIndex].value)">
				<option value="0">Size</option>
				<option value="1">1</option>
				<option value="2">2</option>
				<option value="3">3</option>
				<option value="4">4</option>
				<option value="5">5</option>
				</select>
				<select id="color" class="select" onchange="fontEdit('ForeColor',this[this.selectedIndex].value)">
				<option value="color">Color</option>
				<option style="color: black;" value="black">Black</option>
				<option style="color: red;" value="red">Red</option>
				<option style="color: blue;" value="blue">Bblue</option>
				<option style="color: green;" value="green">Green</option>
				<option style="color: #3b3b3b;" value="pink">Pink</option>
				</select> </div>
<iframe id="textEditor" style="width: 500px; height: 170px;" scrolling="no">
</iframe>

That's it, and we are done. Just run the html page and you can see a nice Rich Text Box. This code is tested in IE 8.0, Firefox 3.6.8 and Google Chrome 10.0.648.204. There are some toolbar image quality issues that you can fix at your end. So start improving Rich Text Box Editor and share it with our community over here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Tarun Dudhatra
Software Developer Working in Ahmedabad, Gujarat, India
India India

I am working as a Software Engineer in Ahmedabad, Gujrat, India.

I have 6+ Years of Experience in Microsoft Technology Like Asp.Net 2.0,Asp.Net 3.0,Asp.Net 4.0,
C#, Javascript, Crystal Reports, JQuery etc.

Find out more on :

Follow on   Twitter

Comments and Discussions

 
Questionbold , edit , underline not working correctly in IE 7,8,9 but working correctly in chrome PinmemberMember 103699149-Apr-14 23:30 
QuestionNeed Help Pinmemberpooja1822-Jan-13 1:30 
nice code of your's...
itz..really helpfull for new devoloper... Smile | :) Thumbs Up | :thumbsup:
but plzzz..will you provide us the .gif (like bold.gif ...) images of bold,italic,underline etc...
AnswerRe: Need Help PinmemberMember 991960517-Mar-13 22:16 
GeneralMy vote of 5 PinmemberUma Shankar Patel8-Feb-12 22:19 
Questionreg: iframe value in codebehind in c# Pinmemberdannavarapuprasad27-Dec-11 21:21 
AnswerRe: reg: iframe value in codebehind in c# PinmemberMember 103699149-Apr-14 23:24 
Questionwant ot retreive from database [modified] PinmemberMember 805020011-Aug-11 21:11 
AnswerRe: want ot retreive from database [modified] PinmemberMember 103699149-Apr-14 23:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 1 Apr 2011
Article Copyright 2011 by Tarun Dudhatra
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid