You need to set up the menu from Wordpress admin.
Nov 20, 2009

Elastic Multiple Select List

Picture 6Following on from my previous post demonstrating my mooSelect class, today I needed to create something different, again with a multiple select list.

This time the brief was to make a standard multiple select list “expand” when it gets focus so as to prevent it from messing up the rest of the page whilst it is not being used.
By default, if a select list does not have a width defined via css, it will simply expand to fit the width of the longest option text.
This can cause problems when the items are excessively long.

My solution was to set a fixed width for the list via css and then use Mootools to detect the length of the longest option.
Then, when the user clicks on the list, it expands out to this width.

See Demo

As I am currently trying to get my head round mootools classes, I just had to do this by creating a class.
To make the class slightly more flexible, there are several options that can be set, possibly the most interesting of which is the possibility to define whether the select list should expand on “mouseover” or on “focus”.

Anyway, here is what I did:

The html:

<select id="mySelect" style="postion:relative;width:100px;" multiple size="8">
<option value="1">Option one</option>
<option value="2">Option two - longer</option>
<option value="3">Option three</option>
<option value="4">Option four</option>
<option value="5">Option five</option>
<option value="6">Option six - this one should define the length</option>
<option value="7">Option seven</option>
<option value="8">Option eight</option>
<option value="9">Option nine</option>

The javascript (mootools)

var mooElasticSelect = new Class({
  Implements: [Options],
    options: {
      charWidth :'8',

  initialize: function(element,options){
    this.selectBox		=	$(element);
    this.selectOptions	=	this.selectBox.getElements('option');;

  build: function(){
    //	wrap list in relative layer to flow "over" items
    var wrapper = new Element('div', {
      'styles': {
        'position': 'relative'

    //	find longest optin text value
    var optionLength=0;
    this.selectOptions.each(function(option) {
      if(lng>optionLength) optionLength=lng;
    var selectWidth=optionLength*this.options.charWidth;

  stretch: function(lng){
    var initLength=this.options.initLength;
        'focus':function(){this.tween('width', ''+lng+'px');},
        'blur':	function(){this.tween('width', ''+initLength+'px');}
        'mouseover'	:function(){ this.tween('width', ''+lng+'px'); },
        'mouseout'	:function(){ this.tween('width', ''+initLength+'px');}

//  initiate the class
window.addEvent('domready', function() {
  var sel =new mooElasticSelect('mySelect',{'charWidth':'6'});


  • […] Elastic Multiple Select List Mootools Splash Screen […]

  • I am trying to use above code but I am getting js error as “Class is undefined”. can you help me how to run sample in my system. I am beginner in javascript.

    • Hi,
      Can you share an url? otherwise it is going to be impossible to help you.

Leave a comment