Instant onChange with IE checkboxes

Browser differences are a daily fact of life for webdevelopers. It is something you either learn to deal with, or it kills you before 40.

Some of these are just plain stupid, but some are simply different ways of interpreting the standard. A good example is the onChange event. This is a simple event, commonly attached to <input /> elements, that fires when the state or value of the element has changed.

For normal text fields, this event fires when the element loses focus. This is logical, since you don’t want it to fire after every keypress. However, for other types of field, the behaviour differs between browsers. Internet Explorer is consistent between types, and always waits till the element loses focus, regardless of the type attribute. Most other browsers however fire the event immediately if the states of the element are clearly defined. Radiobuttons and checkboxes for instance, are either on or off, so the event can fire as soon as the state changes, but IE still waits till the focus is moved to another object.

This leads to problems when the state of a checkbox or radiobutton has influence on other fields in the form. Take for example a situation where the state of a checkbox determines whether a submit button is enabled or not. In IE, the user has to click the checkbox, then click somewhere else in the form, and only then can he submit the form.

The most common workaround for this is to use the onClick event instead of the onChange event, but this will fire on every click, not just if the click actually changes the content.
I recently solved the problem much more elegantly: In addition to the normal onChange event, add an onClick event that removes focus from the element: onclick="this.blur()". When the user clicks the element, it is changed, and focus is immediately moved elsewhere. This means the onChange event is always called immediately, regardless of browser.

An even more advanced version would move the focus to the next element in the form: onclick="focusNext(this)", with the function defined as:

function focusNext(elem)
{
	var formElems = elem.form.elements.length;
	var thisIndex = $.inArray(elem, elem.form.elements);
	var nextIndex = Math.min(formElems-1, thisIndex+1);
	elem.form.elements[nextIndex].focus();
}

Note that this version requires JQuery. Some browsers support the Array.indexOf() method, in which case you could use:

function focusNext(elem)
{
	var formElems = elem.form.elements.length;
	var thisIndex = elem.form.elements.indexOf(elem);
	var nextIndex = Math.min(formElems-1, thisIndex+1);
	elem.form.elements[nextIndex].focus();
}

However, this method is very sparsely supported, and while you can add it yourself via prototype, I find that JQuery is useful enough that I usually add it to my site anyway.


Making sense of ‘Over the hills and far away’

You may or may not know the song ‘Over the hills and far away’ by Gary Moore (there’s also an excellent cover by Nightwish). Instrumentally, this is a very enjoyable song, with good tempo and some great guitar bits. The lyrics however, are slightly odd, and the more you listen to them, the less sense


Growing a ZFS pool

I run several fileservers using the excellent ZFS filesystem. While originally a Solaris invention, it was successfully ported to FreeBSD for version 7. It is extremely well suited for large fileservers due to a combination of reliable software RAID (modes 1, 5, and 6), checksumming and snapshots. Combined with the option of nested filesystems and


Holy Wars: Mac vs PC

I recently got drawn into another ‘Mac vs PC’ argument , which is a bit silly, as I don’t really like Windows, and don’t really mind Macs (to be clear: I’m always on the Windows-side of the argument). The only real problem I have with Macs, are the users. For some reason, pretty much all


2011

I wish you all a Happy New Year.


Replacing bad motherboard capacitors

Now, this is a problem that likely won’t affect you if you either replace your PC every 2 years, or don’t use it very often. However, if you’re like me and build your machines to specs that’ll set you for five years, and then keep them on 100% CPU load 24/7 for those 5 years (thank you,


Useful Unix Units: Screen (part 1)

Ok children, time to learn about screen, one of the most useful little Unix tools available today. First, let me get this straight: I don’t believe in the ‘one OS to rule them all’ crap. Horses for courses, and for server work, my horse rides a command line FreeBSD install. Digression: I’ve never much seen


Logging the backlog

Well, Itsacon’s Log is officially overhauled. For the old registered users, the old website parts are (for now) still available at the familiar links. I’ve imported the old ‘cats’ log into the new site, my photography experiments are mostly over at my Tripodplaces log and everything else was either unfinished or crap, and was ditched.


Status update

Been a while, but the boys have been growing up steadily. Primary activities haven’t changed much though: Sleeping, eating and the occasional tumbling around. By now we’ve grown accustomed to being used as heated mattresses during the night, as well as the fact that entering the kitchen will be greeted by a combined chorus of


Being scared is for pussies…

We stayed at home this new years’ eve, in case the fireworks would launch the boys up the wall, but we needn’t have worried. Ben stoically went on contemplating ways to eat our food, while Sam’s biggest problem was figuring out if the fireworks at the front of the house or those at the back