Jul

19th

jQuery form tooltips

posted on Sunday 19th July, 2009 by

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

new ilikemusic.com website goes live

posted on Friday 30th July, 2010 by neil

We've been busy with the guys from I Like Music over the past few weeks designing and building their new interface. The site is now live and we're all really excited about it. There are some new fe...

more>>

Bluebit rolling out Plesk for all web sites

posted on Wednesday 21st July, 2010 by neil

We are excited to announce that all our hosting customers will be able to use the well respected Plesk control panel provided by Parallels. We're currently rolling out 8.3 on all web sites but looking...

more>>

conGuu 1.13.0 update

posted on Thursday 8th July, 2010 by neil

We've been busy on the rollup to our big V2 launch and there are some more additional features now available in conGuu: Gallery manager - A new module for users who want to set up image galleries...

more>>

Southampton Cycle Challenge

posted on Wednesday 23rd June, 2010 by neil

We're signed up and ready to go on Southampton City Councils new "drive" to get people on their bikes across Southampton and Hampshire. Whilst two of us already cycle to work and a third walks ever...

more>>

conGuu 1.12.1 update

posted on Thursday 10th June, 2010 by neil

We've carried out a few tweaks and modifications in this latest point release. Here's what's new: VAT Support for ConGuu cart. This gives us the ability to set and change VAT rates and display a ...

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