Recently I’ve been trying out the HTML5 Boilerplate to get some of this new fangled HTML into my projects. One thing I particularly like about the boilerplate, is the feature that allows you to not just implement HTML5, but also provide a fallback for the not so fortunate browsers.

Please note, you don’t have to be using the HTML5 Boilerplate to get this to work, simply, some of the great libraries it uses. Go grab yourself:

I’m going to use HTML5’s placeholder attribute for form fields in this example. If you’re not sure what placeholder does, it essentially pre-fills a form input with a piece of text, you click inside it, the hint removes itself and allows you to type, if you click elsewhere and you haven’t typed anything, it comes back!

Before you start, make sure you’ve loaded in the jQuery and Modernizr library, do not load in the Example plugin, we’ll deal with this later.

The HTML5

First, add a form field to your page with a placeholder. That’s your HTML5 business out the way, it will perform nicely in these browsers.

<input name="search" placeholder="Search..." />

The Javascript fallback

Now for the fallback, which is pretty clever. It uses the Modernizr library to check for placeholder support, if it’s not available, perform jQuery’s Ajax call to asynchronously load in the Example jQuery plugin. Once the plugin is loaded we pass the element to fire the plugin on a return it’s placeholder attribute as the text to use for the plugin.

$(document).ready(function(){
	if (!Modernizr.input.placeholder)
	{
	$.ajax({
		type: "GET",
	   	url: "example.js",
	  	dataType: "script",
	  	success: function()
	  	{
	  		$('input').example(function() {
				return $(this).attr('placeholder');
			});
	  	}
	});
	}
});

If anyone wants this code explained in a bit more detail, then let me know in the comments. If not, copy ‘n’ paste and away and improve it!

Author: Benjamin Reid

My name is Benjamin Reid, I live in the UK and keep the magic locked into this site.

RSS Icon

Subscribe to the RSS feed some design goodness, get in on the upcoming software give-aways!

Digg Icon Share Quick Tip #10 - How to get HTML5’s placeholder working across all browsers to Twitter Share Quick Tip #10 - How to get HTML5’s placeholder working across all browsers to Delicious Share Quick Tip #10 - How to get HTML5’s placeholder working across all browsers to Facebook

Related post(s)

Comments left by other wizards

11 Responses | Make a comment

Leave your words of wisdom

Leave a Reply

This is a Gravatar

Your name