Category Archives: Accessibility

Get the text within an element—including alt text—using jQuery

When working on the butterfly lightbox I needed a way to grab the text with a link including the alt text from any images within.

I couldn’t find any existing approaches, so I put together this small plugin to do the job. I thought I should post it here before I forget, or in case anyone else might find it useful too.

Example

If you had the following html:

<a id="smiley" href="">The symbol for happiness is <img src="smiley.png" alt="a smiley face" /></a>

You could grab the accessible text using the jQuery plugin like this:

$('#smiley').accessibleText();

Which would return the text ‘The symbol for happiness is a smiley face‘.

Plugin code

/**
 * jQuery plugin that returns the text nodes within the target element, combined/concatenated 
 * with any alt text or input values.
 */
$.fn.accessibleText = function() {
	if (this.is('img')) {
		return this.attr( 'alt' );
	} else if (this.is('input')) {
		return this.attr( 'value' );
	} else {
		return $.map( this.contents(), function( domElement ) {
			if ( domElement.nodeType === 3 ) {
				return domElement.data;
			} else if ( domElement.nodeType === 1 ) {
				var $element = $( domElement );
				if ( $element.is( 'img, imput' ) || $element.find( 'img[alt], input[value]' ).length > 0 ) {
					return $element.accessibleText();
				} else {
					return $element.text();
				}
			}
		}).join( '' );
	}
};

Let me know if you found this useful, or if you know of a better solution!

Edit: Much improved version above, thanks to Ben!
Edit again: I have updated it again to support input elements, and support using the plugin on inputs and imgs directly (this should really go on github).
Final edit: Here it is, accessibleText on github.

Posted in

ARIA keyboard navigation best practice

I finished updating the ARIA keyboard navigation plugin tonight.

As of this version the plugin manages focus using tabindex alone, which means it functions in browsers old and new (whether they support ARIA explicitly or not). Browser support has been tested with Firefox 3.6.3, Chrome 4, Safari 4, Opera 10.51 and the Trident Trio (IE 6, 7 and 8).

During the refactoring I also managed to shave ~170 lines from total code weight — and this version has more comments!

Posted in

Transformer transformed

In light of increasing browser support for CSS3 media queries, I have spent some time today updating the transformer layout plugin in an attempt to achieve a seamless integration of the new CSS3 functionality for including stylesheets dependent on the browser’s current viewport width.

Most media query implementations I have seen use px units to define min-width and/or max-width limits. This approach is fine for browsers that zoom, but less than ideal for browsers that don’t. Not a huge issue when you consider that the modern browsers that support media queries all zoom by default! But for transformer layout to retain support for older browsers that don’t zoom, em units would be the way to go.

Firstly, I ran some tests of current browser support for CSS3 media queries using em units, the results were:

  • Firefox (3.6.2) and Opera (10) support media queries with em units and load the correct stylesheet after zoom level change.
  • Chrome (4) and Safari (4) support media queries with em units but fail to load the correct stylesheet after zoom level change.
  • IE (6, 7 and 8 ) do not support media queries at all.

Alexander Farkas has a nice solution for detecting media query support and supplementing for non-supporting browsers but unfortunately it doesn’t support em units or compensate for Safari and Chrome’s insufficient handling of zoom level changes.

The new transformer layout plugin takes Alexander’s great work and rectifies both of those limitations so now it can be used to achieve an approach to layout optimised for all browsers, devices, resolutions, text sizes and zoom levels.

Posted in

Accessible UI buttons

Today I updated the compact content widgets such that assistive technologies that don’t appropriately support the title attribute are still able to access the text expansion of each button on the User Interface (UI) for the widget.

While doing this, I thought I should take the time to document the updated approach I use when creating groups of UI elements for my DHTML projects.

Let me know your thoughts.

Posted in

Large content, an eternal golden conundrum

I’ve always been a fan of flexible layout and trying to avoid introducing horizontal scrollbars whenever possible. Large content however poses an eternal conundrum: How to accommodate large content in tight spaces?

Some web designers like to lock a page design to a fixed width (or set a minimum width) forcing a horizontal scrollbar at lower resolutions for the entire site. This approach however does not accommodate some of the larger content types and I feel its a missed opportunity to maximise accessibility. A flexible layout allows your design to be adaptable and optimised for the device/resolution currently being used to view it.

I’ve been playing with some ideas that can be used in conjunction with any layout to accommodate wide content as easily as possible at lower resolutions.

With this example, trying resizing your browser window, make it fairly narrow.

  • Large content such as tables should expose a horizontal scrollbar just below the table at low resolutions.
  • Images will be scaled down to fit the width available automatically. Scaled images also gain the ability to zoom to full size on click (which also introduces a horizontal scrollbar for the image).

The code is not production ready yet (IE6 in particular needs work) but it should give you an idea of where it’s heading.

Let me know what you think!

Posted in