Download @ Forge JS

Clickables

Clickables is a MooTools plugin that allows non-anchor items to clicked like standard links. The first anchor in the element will be used for the link URL.

MooTools Javascript Class

/*
---
description:     Clickables

authors:
  - David Walsh (http://davidwalsh.name)

license:
  - MIT-style license

requires:
  core/1.2.1:   '*'

provides:
  - Clickables
...
*/
var Clickables = new Class({

	//implements
	Implements: [Options],

	//options
	options: {
		elements: 'li',
		selectClass: '',
		anchorToSpan: false
	},

	//initialization
	initialize: function(options) {
		//set options
		this.setOptions(options);
		//set elements
		this.elements = $$(this.options.elements);
		//set clickable
		this.doClickables();
	},

	//a method that does whatever you want
	doClickables: function() {
	
		//for all of the elements
		this.elements.each(function(el) {
		
			//get the href
			var anchor = el.getElements('a' + (this.options.selectClass ? '.' + this.options.selectClass : ''))[0];
		
			//if we found one
			if($defined(anchor)) {
			
				//add a click event to the item so it goes there when clicked. 
				this.setClick(el,anchor.get('href'));
			
				//modify anchor to span if necesssary
				if(this.options.anchorToSpan) {
					var span = new Element('span',{
						text: anchor.get('text')
					}).replaces(anchor);
				}
			}
		},this);
	},

	//ads the click event
	setClick: function(element,href) {
		element.addEvent('click', function() {
			window.location = href;
		});
	}

});

Class: Clickables

Clickables is a MooTools plugin that allows non-anchor items to clicked like standard links. The first anchor in the element will be used for the link URL.

Implements:

Options

Clickables Method: constructor

Syntax:

var clix = new Clickables(options);

Arguments:

  1. options - (object) Options for the class.

Options:

  • elements - (string) A selector string of elements to act as the clickable.
  • selectClass - (string) The CSS class of the anchor/link that should be used for the block's HREF.
  • anchorToSpan - (boolean) Whether or not the original link should be changed to a SPAN element.
© David Walsh 2007-2010. Contact David Walsh. Powered by the remarkable MooTools javascript framework.