Monthly Archive for July, 2004

Oh for more OS like form elements

When you have a check box as part of a form, there is generally some text associated with it, like ‘Yes’ ‘No’, etc. In an operating system environment (Explorer, Finder, Gnome/KDE, etc), the text associated with a check box is usually clickable, the result of which toggles the check box.

HTML forms, on the other hand do not generally have this capability. As HTML forms by default display OS GUI widgets (similar action buttons, drop down menus and the like), this (I think) detracts from the usability of a website.

Fortunately we can use the DOM, a little bit of JavaScript and some little used (it seems) structural markup to rectify this, and make your form more semantically representative of the information it conveys to boot! Let’s get it on.

We’ll start with a basic, unstructured form in minging, dirty HTML. The kind of HTML your grandma would write:


<form action="somewhere.php" method="post">
Box 1: <input name="box1" value="some value" type="checkbox" />
Box 2: <input name="box2" value="some other value" type="checkbox" />
</form>

The first step is to turn it into XHTML while getting rid of those semantically meaningless paragraph tags and replace them with the better smelling label tag (incidentally, I just got back from France to find some rancid chicken in the fridge. That was not a nice smell, but I digress. The HTML above smells worse. See? back on topic..).


<form action="somewhere.php" method="post" id="aForm">     <label for="box1">Box 1:
<input name="box1" id="box1" value="some value" type="checkbox" /></label>
<label for="box2">Box 2:
<input name="box2" id="box2" value="some other value" type="checkbox" /></label>
</form>

O’Reilly has this to say of the label tag:

The label element defines a structure and container for the label associated with an input element. Because the rendered labels for most form controls are not part of the element’s tag, the label attribute provides a way for an author to associate the context of a label with its control.

Additionally, the ‘for’ attribute in the label tag links the label with it’s control.

If your primary audience are Mac/IE 5 users, you can stop here – for them, clicking the text next to the check box will, uh, check the box. It’s almost as if it was planned. The rest of us, unfortunately aren’t so lucky..

Using the DOM, we can find the checkboxes on the page and change their status thus:


var theBox = document.getElementById(boxid);
theBox.checked = (theBox.checked ? false : true);

And then call this JavaScript via the onclick handler thus:


<form action="somewhere.php" method="post" id="aForm">     <label for="box1" onclick="checkTheBox('box1');">Box 1:
<input name="box1" id="box1" value="some value" onclick="return false;" type="checkbox" /></label>
<label for="box2" onclick="checkTheBox('box2');">Box 2:
<input name="box2" id="box2" value="some other value" onclick="return false;" type="checkbox" /></label>
</form>

Note that a handler is placed on the checkboxes themselves that effectively disables them. This is because of a little quirk whereby in most browsers (although not Safari), if you click the checkbox, it will check the box, then run the onclick handler attached to the parent label tag, which will uncheck the box. Or the other way round. Either way, disabling the the checkbox allows the checkbox to function ‘normally’, albeit at the price of altering how forms should work…

To make the form that little bit more semantically correct (and for it to validate), we can add fieldset and an optional legend tags thus:


<form action="somewhere.php" method="post" id="aForm">   <fieldset>
<legend>Check the mofo' boxes</legend>
<label for="box1" onclick="checkTheBox('box1');">Box 1:
<input name="box1" id="box1" value="some value" onclick="return false;" type="checkbox" /></label>
<label for="box2" onclick="checkTheBox('box2');">Box 2:
<input name="box2" id="box2" value="some other value" onclick="return false;" type="checkbox" /></label>
</fieldset>
</form>

That’s it, we’re done. Check out a (mostly – I’ve just this second found that Safari doesn’t like it, but I’ll look at it again tomorrow) working example here.

Popularity: 6% [?]

franceCam

franceCam

Popularity: 5% [?]

Moving on…

Life has changed rather rapidly in the last week for me, so I thought I would have a chat about it with the readers (all five of you) of this fine website.

I started a new job on Monday. 9.30-5.30, in Greenwich, small company, that hires out music and musicians for weddings and other assorted random events. It’s kind of tedious at times, but it’s also fun at times. There are so many things to learn and I’m having trouble remembering where everything is filed, where everything is stored on the computers and all the questions you have to ask people when they ring up. I am sure I’ll get my head round this at some point. But it is terrifying, I finally have responsabilities, I’m bound to make a whole raft load of mistakes, but ultimately I’m going to have to take the blame for all of them. Zoiks!

Then Wednesday comes, frantic text-messaging ensues amongst my group of college friends – they decide to post our exam results early! So, at 5.30 I step onto the bus back to New Cross, and sit nervously in Deptford traffic, envisaging scenes of panic around the boards, and a sense of dread wells up as I imagine not being able to find my number.

There was no one by the boards when I arrived and, despite the lack of crowding, it did take me at least five minutes to find the list of music graduates. Scanning down the list, I eventually found my number in a very pleasing position, and then checked it against my student card at least ten times before it finally sinking in. I had graduated.

Friday brought another change. I left my previous job, a moderately well-paid evening and weekends type thing in one of this cities’ great musical venues. If you made a mistake, you could blame a thousand other people, anyone but yourself. A kind of comfortable, but at times an intenslty uninteresting and unchanging type of comfortable. The evening went slowly and I was due to finish after the interval (and of course head straight to the pub for last night drinking). I asked my manager if it would be possible, since I was leaving, to sneak in at the back and just listen one last time. Better than that, she told me to leave ten minutes before the interval and then grab a ticket and sit in a prime spot in the building. I was amazed. But truly thankful. So I sat and watched, not moving for an hour and a half, entranced, slightly sad, absorbing every last drop.

I never thought I would miss the place, and I don’t think I will. The job essentially entailed being abused by members of the public, mixed in with the occassional friendlier faces. I won’t miss that. But seeing fantastic productions and hearing fanstatic music, as a regular part of your job is something that I am loathed to leave. People too I suppose, I have made some good friends there, and they’ll be missed. I know there will be some of them that I’ll still see from time to time, which will be good.

A week for moving on. I feel like I’ve left school again, or something, faces you’ll never see again dissapear, the bleak smell of most of the classrooms will never be there, and the safeness and security is all gone. Instead, I’ve just left university and started life – something which needn’t be frightening and is simply exciting.

Popularity: 6% [?]

Anniversary, of sorts.

I stopped smoking a year ago today.

Yay, go me.

[update]
6th July 2004

Okay, to be more accurate, the last cigarette i smoked was smoked a year ago today
[/update]

Popularity: 4% [?]