Jul

19th

jQuery form tooltips

posted on Sunday 19th July, 2009 by Bluebit Limited, Southampton, Mark Jones

Just recently someone posed the question to me, "How would I use a span as a Tooltip when someone hovers an input?" I was feeling particularly helpful so I decided to take a few minutes to come up with a solution. Whilst its a simple bit of script, as any developer knows, its sometimes the most simple things that people don't document!

I'm going to be using jQuery to do this, so obviously a bit of knowledge about this library will be useful if you want to completely understand the code, if not, copy, paste and enjoy!

The HTML

The CSS

label { width: 270px; float: left; display: block; padding-left: 5px; }
label span { padding-left: 10px; }
input { float: left; }

As you can see I've made an extremely simple form styled crudely that does nothing. This isnt a tutorial on HTML though so hopefully that can be excused. What we are aiming to do is to display the content of the span tag next to the label when we hover any input with the class tooltip.

The Javascript

$(window).load(function() {
	// hide any spans that are children of a label
	$('label span').hide();
	// attach a function to the hover event on an input with the class tooltip
	$('input.tooltip').hover(function(e) {
		// define a var called inputName as the name attribute of the events target
		inputName = $(e.target).attr('name');
		// select the child span of the label that has a for value that matches the inputName var and show it
		$('label[for='+inputName+']').children('span').show();
	}, function(e) {
		// using the call back function of the hover event select the child span of the label that has a for value that matches the inputName var and hide it
		$('label[for='+inputName+']').children('span').hide();
	});
});

Thats all there is to it! Obviously this can be done in many ways but I thought I'd share this solution. You can see an example of this working here

Follow me on twitter mark_jones

latest news

rss iconWhat we're saying on the blog

Plesk 10

posted on Thursday 26th January, 2012 by Neil

We've recently rolled out our new control panel to all our users, Plesk 10 which is a direct replacement from Plesk 9 that we were currently using. Users will be presented with a brand new inter...

more>>

Opencart ecommerce support and hosting

posted on Tuesday 14th June, 2011 by Neil

We're now offering ecommerce solutions through Opencart, an open source ecommerce platform rich in additional features. Opencart has been around for a few years now and is gaining ground in the ...

more>>

The Internet Explorer 6 countdown

posted on Friday 1st April, 2011 by Neil

Finally Microsoft have decided to visibly let people know of the impending shutdown of Internet Explorer 6. Its a bold move but one that many developers welcome. However there are still many corpor...

more>>

Christmas season

posted on Thursday 16th December, 2010 by Neil

With Christmas well and truly here (well almost), we're preparing ourselves for extended noshing of lovely food, the exchange of presents and the pleasure of good company. I've already added a coup...

more>>

When will Internet Explorer 6 finally die?

posted on Wednesday 15th December, 2010 by Neil

It will be a bitter sweet day indeed when the staple browser for many Windows XP fans out there finally pops its clogs. It's the browser that all of us developers love to hate, with its quirky hand...

more>>
loading icon Loading... Please wait.