jHtmlArea - The all NEW HTML WYSIWYG Editor for jQuery

21. July 2009

The last couple days I spent time working on a new simple, lightweight, extensible HTML WYSIWYG editor that's built on top of jQuery. I know there are a ton of existing editors, but I couldn't seem to find any with a truely simple, lightweight design that allowed for really easy extensibility, and that's built on top of jQuery to take advantage of the cross-platform capabilities that jQuery has to offer. I feel that I've come up with a really nice HTML editor component that has some pretty usefull extensibility points. Allow me to introduce you to jHTMLArea.

You can download jHtmlArea and some examples of using it over at the official project page on CodePlex: http://jhtmlarea.codeplex.com

To the right there's a screenshot of two instances of jHtmlArea in action. The first one is using the "default" configuration, and the second uses a couple of different custom options, including a completely custom "Save" button that's added using one of jHtmlArea's extensibility points.

Using jHtmlArea is As Simple As 1.2.3.

Follow these 3 steps and you'll have jHtmlArea implemented within your application in no time.

1. Add a <TextArea> to your HTML page

<textarea></textarea>

2. Add the "jHtmlArea.js", "jHtmlArea.css" and "jHtmlArea.png" files to your website

3. Add the following JavaScript code to your page to turn all TextArea elements into jHtmlArea's:

Include the jHtmlArea javascript file:

<script type="text/javascript" src="jHtmlArea-0.6.0.min.js"></script>

Turn all TextArea’s into jHtmlArea’s:

$(function(){
    $("textarea").htmlarea();
});

It can be as simple as that to use jHtmlArea within your pages to turn any TextArea DOM Elements into nice jHtmlArea WYSIWYG Editors.

Easily Configure Toolbar Buttons

jHtmlArea makes it extremely simple to define your own custom set of Toolbar buttons; just in case you don't want to show the full set of "default" buttons.

You can easily specify them by name within an array that's passed in as one of the options specified when you call the "htmlarea" method:

$("#txtCustomHtmlArea").htmlarea({
    toolbar: ["bold", "italic", "underline", "|", "h1", "h2", "h3", "h4", "h5", "h6", "|", "link", "unlink"]
});

This example will specify the same buttons displayed in the lower screenshot to the right; minus the "Save" button on the far right of the toolbar. I'll show you how to add this button in the next example.

Add Custom Toolbar Buttons

One of the extensibility points that I was wanting to have in an HTML WYSIWYG editor is the ability to easily add any custom buttons to the toolbar. For instance, some times it may be nice to have a "Save" button in the toolbar to allow your users to easily save the contents of the editor.

Here's an example using the above "custom" toolbar buttons list, with a custom "Save" button added:

$("#txtCustomHtmlArea").htmlarea({
    toolbar: ["bold", "italic", "underline", "|", "h1", "h2", "h3", "h4", "h5", "h6", "|", "link", "unlink", "|",
        // This is how to add a completely custom Toolbar Button
        {
            // The CSS Class to assign the Button <a> tag
            css: "custom_disk_button",

            // The Text to display in the buttons alt text / tooltip
            text: "Save",

            // The function to execute when the button is clicked
            action: function(btn) {
                // 'this' = jHtmlArea object
                // 'btn' = jQuery object that represents the <A> "anchor" tag for the Toolbar Button

                alert('SAVE!\n\n' + this.toHtmlString());

                // Add any Ajax Code here to save the contents of the editor

            }
        }
    ]
});

Once the JavaScript code is entered to add the custom "Save" button, we then need to add the buttons display image to the website, and then include the necessary CSS to allow it to be displayed:

<style>
div.jHtmlArea .ToolBar ul li a.custom_disk_button 
{
    background: url(images/disk.png) no-repeat;
    background-position: 0 0;
}
</style>

What about Localization?

Well, the jHtmlEditor currently only comes with English text for all the button names / tooltips. However, it is extremely simple to specify your own text to use when calling the "htmlarea" to create jHtmlArea editors within the page. You set the Text to be displayed for each button by referencing it by "name". This is the same "name" that is used in the first example to specify which buttons are displayed in the toolbar.

$("#txtCustomHtmlArea").htmlarea({
    // Override any of the toolbarText values - these are the Alt Text / Tooltips shown
    // when the user hovers the mouse over the Toolbar Buttons
    // Here are a couple translated to German, thanks to Google Translate.
    toolbarText: $.extend({}, jHtmlArea.defaultOptions.toolbarText, {
            "bold": "fett",
            "italic": "kursiv",
            "underline": "unterstreichen"
        })
});

Specify CSS File to be used by the Editor

You can also specify a specify CSS file to be used within the Editor itself.

$("#txtCustomHtmlArea").htmlarea({
    css: "style//jHtmlArea.Editor.css"
});

Specify a Callback when the Editor Finishes Loading

It may be nice in some instances to get notified when the editor has finsihed loading in the page so you can perform some kind of action. This is also really simple to do.

$("#txtCustomHtmlArea").htmlarea({
    // Do something once the editor has finished loading
    loaded: function() {
        //// 'this' is equal to the jHtmlArea object

        alert("jHtmlArea has loaded!");

        //this.showHTMLView(); // show the HTML view once the editor has finished loading
    }
});

Call any jHtmlArea object methods easily from jQuery

Another thing is that the jQuery object doesn't directly expose the different jHtmlArea object's methods. However, you can use the "htmlarea" method to call any of them you need to. All you need to do is specify the method by name that you want to call and pass any additional parameters to the "htmlarea" method and it'll return the results.

// Get the HTML string value from within the editor
var html = $("#txtCustomHtmlArea").htmlarea("toHtmlString");

// Insert a specify Image that you want
$("#txtCustomHtmlArea").htmlarea("image", "image.jpg");

The jHtmlArea object has a few more methods, you can find out what they are by referencing the Visual Studio JavaScript Intellisense File ("jHtmlArea-0.5.0-vsdoc.js") or the un-minified source code itself.

Conclusion

As you can see the jHtmlEditor has some really simple extensibility points. I hope you enjoy using this editor as much as I do. Also, if you have any suggestions/issues, please go to the official jHtmlArea project's Issue Tracker and let me know.

JavaScript , , ,

Comments

7/27/2009 11:29:55 AM #
nerdok, I just created a "live" demo page here: http://pietschsoft.com/demo/jHtmlArea/

Thanks for the suggestion!
7/30/2009 8:32:50 PM #
jHtmlArea - New HTML WYSIWYG Editor

You've been kicked (a good thing) - Trackback from DotNetKicks.com
7/31/2009 10:58:29 PM #
Robert, I haven't specifically built-in any accessibility features, however the Toolbar Buttons of the control are <A> tags with the Title attribute set, so that should be "accessibility friendly". Please post any suggestions you have for things to add/change to the Issue Tracker or Discussion Forums. Thanks.
Iamnotarobot
Iamnotarobot
8/4/2009 11:45:08 AM #
LOL at the SPAM comments!  Smile

Looks like a nice and simple editor (a lot of the other ones have too many options).  I'll bookmark this page and come back if I need an HTML editor in the future.
8/10/2009 11:07:01 AM #
ITmeze, Thanks! I created jHtmlArea with the idea of simplicity and you example is proof of that.
Jon
8/15/2009 7:31:49 PM #
Hi Chris,

Just did a search for a simple and extensible jquery based text editor and guess what I found. Really love the control and thinking about using it in a couple of projects but have a couple of questions.

1) I looked at the Microsoft Public License and it looks like it's free to use as long as we include any notices if redistributing it. I want to use a minified copy of the plugin in sites that we sell, so is this OK?

2) I notice it's version 0.6. Do you have any plans to continue development, if so what difference is likely between this and the first full version?

Thanks again for your great work on this plugin. Jon.
Vincent D'Souza
Vincent D'Souza
8/16/2009 11:07:24 PM #
Hello Chris,

You have done a great job explaining everything but since I am totally new to jQuery world finding little hard time using this feature.  Even though I followed each and every step you explained I do not get to see the toolbar etc.

Is it possible to post the entire code that you used in your demo somewhere here so that I can compare what am I doing wrong, please?

I greatly apprecaite if you could do that and I am pretty sure there are tonns people who would thank for this too.  Smile

Thanks,

Vincy
Vincent D'Souza
Vincent D'Souza
8/16/2009 11:23:49 PM #
Hi Chris,

Some how I figured it out and its working but have one suggestion.  When I click on the "Insert Image", it gives me the nice browse functionality and that is what I was looking for in the HTML Editor.

But the problem is if I select any image file from my local harddrive, it doesn't upload to the server and keeps the local physical path in reference.  Is it possible to the moment I click on the "OK" button on the "Insert Image" dialogue it upload the file to the server?  Also, need the property to set the location at the server.

If I have that feature then I will be a happy man. Smile  If you could add that, that would be great or if you have any suggestions how to do that that is good too.

Thank you for your good work and look forward to hear from you,

Vincy
8/22/2009 5:21:04 AM #
Hey thanks for a great widget Chris!

I was wondering if anyone's managed to get a demo working
using a ColorPicker as a custom toolbar font-colour button as this would be a really great feature for this widget!

Cheers,
webCV
8/22/2009 5:27:05 AM #
Sorry just noticed its here, awesome!! pietschsoft.com/demo/jHtmlArea/ColorPickerMenu.htm

Just a couple of suggestions:
- When I select the color picker, then want to cancel, hitting Escape / clicking somewhere else doesn't remove the colPicker (I'm using FF2.0 on kubuntu hardy)
- Perhaps a 'Custom' colour option can be added in the colour picker which will let you enter html code? (I guess they can do this via html anyway)

Cheers!
G
Sam
Sam
8/24/2009 2:33:35 AM #
Pushing enter should create a P element. Shift+Enter should create a br element. Its the universal standard.
Max
Max
8/27/2009 10:08:36 AM #
Hi, i'm using this jquery but i found a problem.
I can't pre-populate the text area.
If i'm in "HTML mode" i can do it, but in "text mode" not.
Any suggest?

Thanks
Max
patricia
patricia
8/29/2009 12:28:37 PM #
first thing, great control, nice and simple to implement and i like the flexibility it offers.   One thing i'm having trouble with is having a lot of buttons in the toolbar, it doesn't start a new tool bar, so i have buttons with out backgrounds in the second row.

here's a picture to show what is happening, b/c i don't think my description really explains it very well.

img263.imageshack.us/img263/4157/jhtmleditor.png

Thanks for the great control!
9/17/2009 3:04:26 PM #
I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.
jeff
jeff
9/17/2009 5:42:14 PM #
doesn't work. you can't submit through a form. none of the information in the text area (unless it's preassigned in the html code) get's passed via post.  also, if you submit the form using javascript or a html button.
9/20/2009 8:02:46 AM #
I have the same complain that @jeff does. The post doesnt contain the contents of the control, seems you have to do something like this;
            $("#save").click(function() {
                var html = $("#Comments").htmlarea("toHtmlString");
                $("#Comments").val(html);
                $("form:first").submit();
                self.close();
            });
That has a horrible code smell to it. Once this is ironed out this will truly rock.
10/21/2009 3:49:35 AM #
Hi Chris,

thanks for the great work. I'm just a bit concerned about the HTML that is generated by your plugin.

Why don't you make use of the p-tag instead of divs and brs?
7/24/2010 11:11:20 PM #
Pingback from reymundolopez.com

Editor de texto enriquecido jHtmlArea (WYSIWYG) para ASP.NET y jQuery | Reymundo Lopez
8/17/2011 12:32:49 AM #
Pingback from programmersgoodies.com

Binding onChange Event to jHtmlArea textarea - Programmers Goodies