Add a Text-Sizing Toolbar to Web Forms

Add a Text-Sizing Toolbar to Web Forms

Insert buttons before <textarea> elements to make the text larger or smaller.

I spend a lot of time—probably too much time—commenting on weblogs and web-based discussion forums. Despite several attempts to create some sort of universal commenting API, virtually all of these sites continue to use a simple web form with a <textarea> element for entering comments.

This hack alters web forms to add a toolbar above every <textarea> element. The toolbar lets you increase or decrease the text size of the <textarea>, without changing the style of the rest of the page. The buttons are fully keyboard-accessible; you can tab to them and press Enter instead of clicking them with your mouse.

I mention this up front, because accessibility matters, and also because it was harder than it sounds.

The Code

This user script runs on all pages. The code looks complicated, and it is complicated, but not for the reason you think. It looks complicated because of the large multiline gibberish-looking strings in the middle of it. Those are data: URIs, which look like hell but are easy to generate.(See "Embed Graphics in a User Script" [Hack #11] for more on data: URIs.)

The toolbar is displayed visually as a row of buttons, but each button is really just an image of something that looks pushable, wrapped in a link that executes one of our JavaScript functions. Since we'll be creating more than one button (this script has only two, but you could easily extend it with more functionality), I created a function to encapsulate all the button-making logic:

	function createButton(target, func, title, width, height, src)

The createButton function takes six arguments:


An element object; the <textarea> element that this button will control.


A function object; the JavaScript function to be called when the user clicks the button with the mouse or activates it with the keyboard.


A string; the text of the tool tip when the user moves her cursor over the button.


An integer; the width of the button. This should be the width of the graphic given in the src argument.


An integer; the height of the button. This should be the height of the graphic given in the src argument.


A string; the URL, path, or data: URI of the button graphic.

Creating the image is straightforward, but creating the link that contains the image is where the real complexity lies:

	button = document.createElement('a');

	button._target = target;

	button.title = title;

	button.href = '#';

	button.onclick = func;


There are two things I want to point out here. First, I need to assign a bogus href attribute to the link; otherwise, Firefox would treat it as a named anchor and wouldn't add it to the tab index (i.e., you wouldn't be able to tab to it, making it inaccessible with the keyboard). Second, I'm setting the _target attribute to store a reference to the target <textarea>. This is perfectly legal in JavaScript; you can create new attributes on an object just by assigning them a value. I'll access the custom _target attribute later, in the onclick event handler.

If you read Mozilla's documentation on the Event object, you'll see that there are several target-related properties, including one simply called target. You might be tempted to use to get a reference to the clicked link, but it behaves inconsistently. When the user tabs to the button and presses Enter, is the link, but when the user clicks the button with the mouse, is the image inside the link! In any case, event.currentTarget returns the link in all cases, so I use that.

See for documentation on the Event object.

Now the real fun begins. (And you thought you were having fun already!) I need to get the current dimensions and font size of the <textarea> so that I can make them bigger. Simply retrieving the appropriate attributes from (,, and will not work, because those only get set if the page actually defined them in a style attribute on the <textarea> itself. That's not what I want; I want the final style, after all stylesheets have been applied. For that, I need getComputedStyle:

	s = getComputedStyle(textarea, ""); = (parseFloat(s.width) * 1.5) + "px"; = (parseFloat(s.height) * 1.5) + "px"; = (parseFloat(s.fontSize) + 7.0) + 'px';

Finally, do you remember that bogus href value I added to my button link to make sure it was keyboard-accessible? Well, it's now become an annoyance, because after Firefox finishes executing the onclick handler, it's going to try to follow that link. Since it points to a nonexistent anchor, Firefox is going to jump to the top of the page, regardless of where the button is. This is annoying, and to stop it, I need to call event.preventDefault( ) before finishing my onclick handler:


All this was just for the sake of keyboard accessibility. What can I say? Some people build model airplanes. I build accessible web pages.

Save the following user script as zoomtextarea.user.js:

	// ==UserScript==

	// @name Zoom Textarea

	// @namespace

	// @description add controls to zoom textareas

	// @include *

	// ==/UserScript==

	function addEvent(oTarget, sEventName, fCallback, bCapture) {

		var bReturn = false;

		if (oTarget.addEventListener) {

			oTarget.addEventListener(sEventName, fCallback, bCapture);

			bReturn = true;

		} else if (oTarget.attachEvent) {

			bReturn = oTarget.attachEvent('on' + sEventName, fCallback);


		return bReturn;


	function createButton(elmTarget, funcCallback, sTitle, iWidth, iHeight, urlSrc) {

		var elmImage = document.createElement('img');

		elmImage.width = iWidth;

		elmImage.height = iHeight; = = "1px solid #ccc"; = = "1px solid #888"; = "2px";

		elmImage.src = urlSrc;

		var elmLink = document.createElement('a');

		elmLink.title = sTitle;

		elmLink.href = '#';

		addEvent(elmLink, 'click', funcCallback, true);


		return elmLink;


	var arTextareas = document.getElementsByTagName('textarea');

	for (var i = arTextareas.length - 1; i >= 0; i--) {

		var elmTextarea = arTextareas[i];

		function textarea_zoom_in(event) {

			var style = getComputedStyle(elmTextarea, ""); = (parseFloat(style.width) * 1.5) + "px"; = (parseFloat(style.height) * 1.5) + "px"; = (parseFloat(style.fontSize) + 7.0) +


			event.preventDefault( );


		function textarea_zoom_out(event) {

			var style = getComputedStyle(elmTextarea, ""); = (parseFloat(style.width) * 2.0 / 3.0) +

	"px"; = (parseFloat(style.height) * 2.0 / 3.0) +

	"px"; = (parseFloat(style.fontSize) - 7.0) +


			event.preventDefault( );






				'Increase text size',





















				'Decrease text size',





















Running the Hack

After installing the user script (Tools Install This User Script), go to At the bottom of the page is a form for submitting comments. Above the form, you will see two buttons inserted by the user script, as shown in Figure.

Type some text into the <textarea>, then click the first button (titled "Increase text size") to make the text and the <textarea> larger, as shown in Figure. Alternatively, while focus is in the <textarea>, you can press Shift-Tab twice to set focus to the Zoom In button, and then press Enter to activate the button.

Click the second button, titled "Decrease text size," to make the text smaller. Due to rounding, if you repeatedly zoom in and then repeatedly zoom out, the text and its surrounding box may end up a slightly different size. The zooming is not permanent, so you can refresh the page to return to the original size.

Zoom toolbar in web form

Zoomed web form

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows