Multi-Select Transfers Using MooTools 1.2

Written by David Walsh on Tuesday, June 24, 2008


This article may feature code that is no longer best practice in MooTools.
Click here to learn what has changed to make your code framework-compatible.

While I was a Zone Leader for DZone’s Ajax, CSS, and PHP Zones, DZone’s Rick Ross asked me to contact Jeremy Martin with regard to a hot blog post he created: Easy Multi Select Transfer with jQuery. Both Rick and I were impressed with Jeremy’s script and asked him to repost on DZone.

Fast forward five months later, I decided that I simply had to create a MooTools script that would accomplish the same goal. I mean, if Jeremy’s going to port the Kwicks effect to jQuery, why can’t I take one back for the Moo team? In the end, the MooTools code looks very much like jQuery’s code.

The HTML

<div class="holder">
	<select multiple="multiple" id="select1" name="select1[]">
		<option value="1">Option 1</option>
		<option value="2">Option 2</option>
		<option value="3">Option 3</option>
		<option value="4">Option 4</option>
	</select>
	<a href="javascript:;" id="add">add >></a>
</div>
<div class="holder">
	<select multiple="multiple" id="select2" name="select2[]">
		<option value="5">Option 5</option>
		<option value="6">Option 6</option>
	</select>
	<a href="javascript:;" id="remove"><< remove</a>
</div>

The MooTools Javascript

window.addEvent('domready', function() {
	$('add').addEvent('click', function() {
		$('select1').getSelected().each(function(el) {
			el.inject($('select2'));
		});
	});
	$('remove').addEvent('click', function() {
		$('select2').getSelected().each(function(el) {
			el.inject($('select1'));
		});
	});
});

All we need to do is move the selected <option> elements to the other select list.

Note that the PHP $_POST variable receives the values correctly. Very practical and functional!


Epic Discussion

Commenter Avatar June 24 / #

Ha looks good… and only 4 lines longer! :p No but seriously, I think it’s time for a new library… how about MooQuery?

David Walsh June 24 / #
david says:

@Jeremy: Don’t tempt me to try to make it shorter!

Commenter Avatar June 24 / #
thomasd says:

Sorry, but I had to do this!!! :)

window.addEvent(‘domready’, function(){
$(‘add’).addEvent(‘click’, function(){
$(’select1′).getSelected().inject($(’select2′));
});
$(‘remove’).addEvent(‘click’, function(){
$(’select2′).getSelected().inject($(’select1′));
});
});
8 Lines

or even shorter

window.addEvent(‘domready’, function(){
var move = function(from, to){$(from).getSelected().inject($(to));}
$(‘add’).addEvent(‘click’, move.pass(['select1', 'select2']));
$(‘remove’).addEvent(‘click’, move.pass(['select2', 'select1']));
});
5 Lines

JS-Frameworks rock!!!

David Walsh June 24 / #
david says:

thomasd 1, David 0. Awesome work!

Commenter Avatar June 24 / #

Ok, I’ll admit the ternary usage is a little ridiculous here, but here’s 5 lines in jQuery:

$().ready(function() {
  $(‘#add, #remove’).click(function() {
    $(‘option:selected’, $(this).is(‘#add’) ? $(‘#select1′) : $(‘#select2′)).remove().appendTo($(this).is(‘#add’) ? $(‘#select2′) : $(‘#select1′));
  });
});

Commenter Avatar June 24 / #
thomasd says:

This is what Jeremy’s very nice solution looks like in Mootools 1.2 (of course without the underscores :) ):

window.addEvent(‘domready’, function(){
__$$(‘#add, #remove’).addEvent(‘click’, function(){
____$(this.match(‘#add’)?’select1′:’select2′).getSelected().inject($(this.match(‘#remove’)?’select1′:’select2′));
__});
});

Though I would prefer the other 5 lines solution, as in my opion it is much clearer and ‘move’ can be used for every pair of selects.

@david:
thank you very much for this blog, as it is a daily pleasure for me.
I already use your Generic-Class, and some other ideas posted here.
Greets from Austria

Commenter Avatar June 25 / #
Nate says:

I have been working on the same kind of widget for the past week, using jquery. I had seen the one from Jeremy when it first came out, but it doesn’t do nearly enough for my purposes. Mine does the same basic things as yours, only it works on reset, keeps items sorted when adding and removing, handles preselected items, and only requires a user to create one select box with the class multiple, then the code creates the second box and all the buttons.

I’d post the code, but it is a little over 100 lines, so it’s not exactly an easily readable snippet. If you are interested let me know and I’ll try to find a way to post it.

David Walsh June 25 / #
david says:

@Nate: Feel free to post a link to an example.

Commenter Avatar June 25 / #
Nate says:

I’ll try to set something up when I get home.

Commenter Avatar June 27 / #
Nate says:

I finally got around to posting it:

http://www.suptin.com/selector.html

If you view the source of the js file it will be a huge mess of stuff, and most of it is unrelated to the select transfer thing. Lines 4470 to 4552 contain the majority of the code related to the selector. The lines 4651 to 4654 are in an ready block that initializes the selectors.

Everything is much more organized, when I’m working on it, but I grabbed the build rather than the src. Also the code for the selector would be quite a bit cleaner if I didn’t have to worry about IE 6/7.

David Walsh June 29 / #
david says:

@Nate: Great work. Thank you for posting it.

Commenter Avatar June 29 / #
Peter says:

Hi David,

I like this script. I’ll try and integrate it in a little project of mine.

Otherwise, how does the code hightlighting on this blog work? I’ve been looking for a while and I think ones is pretty neat.
Mind sharing a little info on it?

Cheers,
Peter

Commenter Avatar June 29 / #
Peter says:

Me again…

Answered my own question: you use wmd-editor.com

Now I’ve got to figure how to input code

Commenter Avatar August 19 / #
James says:

anyone know of a drag and drop version of this?

Cheers.

Commenter Avatar August 27 / #
Ch@Sky says:

Exellent David!! thnks a lot.

Commenter Avatar August 28 / #
sophy says:

I need the same tutorial with mootools 1.11
please help

Commenter Avatar August 28 / #
Paul says:

Where should I look if I wanted a 3 box multi select? Basically The first box contains top level categories, then when you select one of those categories, the second box loads up (via ajax) all the sub categories, and then the user can choose to transfer one of the sub categories across to the third and empty box.

Any ideas?

Commenter Avatar September 02 / #
alphanono says:

Wow … great !

But … an other chalange now ?

There is a great plugin for jQuery not yet for mootools : http://code.google.com/p/jquery-asmselect/

Commenter Avatar December 17 / #
Scott says:

Is it possible to limit the amount you can transfer into the second select.

Commenter Avatar February 07 / #
Ajans says:

Thanks you too much, wonderful !

Commenter Avatar June 10 / #
Levis says:

Thank you David for this.

I’ve added a dblclick on the option if someone wants it:

window.addEvent(‘domready’, function() {
$(‘element_add’).addEvent(‘click’, function(){
$(‘element1′).getSelected().inject($(‘element2′));
});
$(‘element_rem’).addEvent(‘click’, function(){
$(‘element2′).getSelected().inject($(‘element1′));
});
$$([$('element1'), $('element2')]).each(function(ex){
ex.getChildren().each(function(el){
el.addEvent(‘dblclick’,function(){
this.inject((this.getParent().get(‘id’)==’element1′ ? $(‘element2′) : $(‘element1′)));
});
});
});
});

Commenter Avatar September 11 / #
Timo says:

Well i played with inject and swapping options between selectboxes way back in my online config editor.

What i came up with is putting it into a Hash (for global closed namespace – no other reason) where i store some helper functions and simply dynamicly call it

var APF = new Hash({
swapSB: function(s,t){ $(s).getSelected().inject($(t)); }
// alot more functions here …
});

and then call APF.swapSB(’source’,'target’) onClick. This way you do not have to bind alot of events on domready but can use this where and when it’s needed. Basicly the way thomasd went using his move function on domready.

Commenter Avatar October 08 / #

Hi

Is it possible on form submit that you can select all options in the second list.

Commenter Avatar February 09 / #
Avinash says:

Hi, David.

Nice script.

But how about the drag and drop version?

Thanks..

Be Heard!

I want to hear what you have to say! Share your comments and questions below.

Name*:
Email*:
Website:  


© David Walsh 2007-2010. Contact David Walsh. Powered by the remarkable MooTools javascript framework.