Javascript


It gave me migraines till I discovered jQuery. Now I can validate a form with some simple CSS and a couple of lines of jQuery code. And today I had a rather odd requirement to toggle the colour of cells in a table when you click on them. In the old days, I wouldn’t even have attempted this. In my new jQuery heaven, I did this:


(document).ready(function() {
$("td.standard").click(
function(){
var currentclass = $(this).attr("class");
if (currentclass.indexOf("highlightcell") == -1) {
$(this).addClass("highlightcell");
} else {
$(this).removeClass("highlightcell");
}
}
);
});

VoilĂ !

Scriptaculous is one of the better-known Javascript/AJAX libraries, with useful built-in objects for adding Web 2.0 functionality to your pages. I’ve tinkered with it but hadn’t found a real-life application for it until I needed to allow a client to reorder a grid of thumbnail photos by dragging and dropping them. Added complication: she uses Safari.

Oh how I hate coding Javascript! I got something kind-of working, including an Ajax callback to update the database, but it wasn’t very satisfactory. So I put it aside for a while and looked at it again when I had some spare time. This time I came across Greg Neustaetter’s PHP wrapper class for Scriptaculous lists. Aha, the power of Scriptaculous without the pain of Javascript! With the aid of his well-documented examples it only took me about half an hour to create a proof-of-concept. So Web 2.0 here I come …