Sunday, November 21, 2010

9:32 PM
There's nothing like a subtle, slick website widget that effectively uses CSS and JavaScript to enhance the user experience.  Of course widgets like that take many hours to perfect, but it doesn't take long for that effort to be rewarded with above-average user retention and buzz.  

One of the widgets I love is Twitter's "Follow" button.  Let me show you how you can implement this functionality with three popular JavaScript toolkits:  MooTools, jQuery, and Dojo.
Note:  This tutorial will only display the client side handling of the form submission — NOT any PHP/MySQL/server-side handling of the request.

The HTML Structure

<form class="follow-form" method="post" action="twitter-follow.php">
<input type="hidden" name="followID" value="123456">
<button type="submit" value="Actions" class="btn follow" title="123456">

The HTML for the button is very simple.  The structure revolves around a BUTTON element which contains an I element and SPAN element.  You're probably thinking "An I element?  WTF."  I know I did.  The truth of the matter is that the I element is deprecated and, as far as I'm concerned, and be used for any purpose the developer would like.  I'm also sure that Twitter doesn't mind saving bytes here or there

The CSS Styles

/* twitter button and its children */
button.btn { 
background-position:0 0;
border:1px solid #ddd;
border-bottom:1px solid #ccc;
font-family:"Lucida Grande",sans-serif;
padding:4px 8px 5px 8px;
text-shadow:1px 1px 0 #fff;
button.btn:hover {
border:1px solid #999;
background-position:0 -6px;
button.btn:active {
background-image:none !important;
text-shadow:none !important;
button.btn i {
background-position:-176px -32px;
button.btn { background:url(; }

/* properties for the element that is generated *after* following */
span.following {  background:#ffd; padding:5px 10px; }
span.following span { width:10px; height:9px; margin-right:5px; display:inline-block; background:url("") -160px -16px no-repeat; }

Most of the styling for this button goes onto the BUTTON element itself.  You'll notice directives to round the button;  leaving the button sharp also please the eye.  Through the regular, hover, and active button states, check out how Twitter users the background position and colors to nicely modify the button without the need for additional images.  You'll also notice Twitter uses should you.

The MooTools JavaScript

/* with mootools */
window.addEvent('domready',function() {
/* fetch elements */
$$('form.follow-form').each(function(form) {
/* stop form event */
form.addEvent('submit',function(e) {
/* stop event */
if(e) e.stop();
/* send ajax request */
var i = form.getElement('i');
new Request({
url: 'twitter-follow.php',
method: 'post',
data: {
followID: form.getElement('input').value
onRequest: function() {
onSuccess: function() {
var button = form.getElement('button');
new Element('span',{
html: '<span></span>Following!',
'class': 'following'
onComplete: function() {

The first step is grabbing all of the FORM elements with the follow-form CSS class.  Upon form submission, the default submission action is stopped.  An AJAX request is fired, using the INPUT element's ID as the user to follow.  When the request is fired, the I element's background image is set to the spinner.  When the request is complete, the button is hidden and a new SPAN element is displayed informing the user that they are now following the given user!

The jQuery JavaScript

// Idiomatic jQuery by Doug Neiner
jQuery(function ($) {
/* fetch elements and stop form event */
$("form.follow-form").submit(function (e) {
/* stop event */
/* "on request" */
/* send ajax request */
$.post('twitter-follow.php', {
followID: $(this).find('input').val()
}, function () {
/* find and hide button, create element */
 .after('<span class="following"><span></span>Following!</span>');

The code above is based off of the MooTools code.  The workflow is exactly the same.

The Dojo JavaScript

/* when the DOM is ready */
dojo.ready(function() {
/* fetch elements */
dojo.query('form.follow-form').forEach(function(form) {
/* stop form event */
dojo.connect(form,'onsubmit',function(e) {
/* stop event */
/* active class */
/* get button */
var button = dojo.query('button',form)[0];
/* ajax request */
form: form,
load: function() {,'display','none');
innerHTML: '<span></span>Following',
className: 'following'

The code above is based off of the MooTools code.  The workflow is exactly the same.

This "Follow" button is only one of many details that Twitter has paid attention to, just to make the user experience on the site better.  Take note from the effort put forth by large websites — adding these types of details to your smaller websites can make the user experience much better for YOUR users!