Browsed by
Author: Matt

jQuery tooltips using ajax

jQuery tooltips using ajax

So you’ve got some tooltip text you want to show your user, but it’s a bit expensive to generate that text and you only really want to go to the bother of generating it when the user invokes the tooltip.

In my situation this was a bunch of numbers on a page, each of which had been calculated by a particularly long and complicated piece of business logic, and the users need to let the page ‘show its working’ now and again.  On a busy page, the agreed UI solution was to put the detail into a tooltip, but I didn’t want to have to pre-populate every cell with a tooltip, when the users probably wouldn’t bother looking at it at all, and when they did they’d only be interested in one or two cells.  So…

HTML: (on my page this bit is repeated many times)

<span
  class="tooltip"
  title="Default tooltip for when the ajax call fails"
  data-id="${line.someId}">${complicatedNumber}</span>

jQuery:

$("span.tooltip").tooltip({
	content: function( event, ui ) {
		var element = $( this );
		var id = element.attr("data-id");
		var tooltiptext = "Default tooltip";

		// Non-async ajax is deprecated, but we're using it
		// here because  we need to get the tooltip text
		// synchronously so that the response is processed
		// in time.
		$.ajax({
			async:false,
			dataType: 'text',
			url:'/tooltip/rest/' + id,
			type:'get',
			success: function(response) {
				tooltiptext = response;
			}
		});
		return tooltiptext;	   
	}
});
Scotney Castle

Scotney Castle

Intentionally ruined when the newer, bigger house was built at the top of the hill, Old Scotney Castle is now just a rather extravagant garden feature.  Where you or I may have a gnome, the aristocracy have castles…

Windy Widemouth

Windy Widemouth

The remnants of Hurricane Bertha hit whilst we were away. We spent the morning acting as ballast in the tent to stop it blowing away, but ventured down to the beach at Widemouth Bay once the wind died down a bit. In splendidly British fashion, we splashed about a bit as people in full wet-weather gear walked past, bemused and amused.

It was quite lumpy.

IMG_0570

Boscastle

Boscastle

That place no-one had heard of until it got massively flooded in August 2004.  We went there last year and kept driving as it was wet and windy.  This time it was just windy so we parked at the (unflooded, rebuilt) car park, and walked up the coastal path for a bit.  We didn’t expect to get to the folly at the top of the headland, but once you get a little bit closer it’s hard to resist…

IMG_0786

Make your own rainbows

Make your own rainbows

This was the rather nicely timed Typhoon display during the Goodwood Festival of Speed.  He appeared just as one of the Nobles cleared out the hay bales at Molecombe and everything stopped for a while.IMG_9399