Basically I wanted to write this plugin since long long time but could not get a while to hold and push my thoughts as a code to form this plugin. As the name suggests (which seems kind of difficult to understand the real purpose of this plugin) it does not deselect existing option when one selects another option in multi-selection list/combo-box. How does this sound? Yes, w/o having your control key pressed. Basically I have been found of changing the default behavior of the html controls and with this new jQuery Magnet Multi-select List plugin, I will continue this trend. If you have already understood the meaning and purpose of this plugin you can move to plugin-usage without reading more boarding story about the plugin.
One would say that it's very easy you just have to hold the CTL key while selecting another option in combo-box but it's going to be very irritating when you have hundreds of options in your list and you have to select multiple out of them one of them is on top and the other one at the end of list, which makes it keeping that CTL key difficult and if one misses out CTL hold for a while and clicks he's gone. He has to start from all over again. To get rid of this problem many web-developers implement their lists/combo-box in ways where one has to click > select and move options from left-to-right and those on the right-side are the ones which are being selected by users. But this plugin will make your life as a developer and users life much easier by not dropping down the selected option each time they select another. Basically this will help many users who are less-friendly with computers and web in general and this plugin development with jQuery came in mind just out of need for such a plugin who can do so and as per my knowledge so far we don't have such plugin available for any javascript library.
Learning from the past experiences, this plugin will support accessibility from the beginning. Yes, there is a keyboard support too.
To use DG Magnet Combo plugin just add following code in your javascript:
$(document).ready(function(){ $("#my_list").dgMagnetCombo(); });
See live demo here or goto github repository to download or fork.
Update: Plugin updated to 1.1 with following changes:
- Used inbuilt javascript/jQuery functions instead of self-made.
- Made it work with CTL key pressed. So now it works normal when someone uses it with CTL key pressed and then takes back (in hybrid mode)
Todo: Need to verify and test SHIFT key compatibility to make it work in natural mode when used with SHIFT key.
Bugs: Please let me know if you encounter any problem/bug while using this plugin. Either post comment on this blog or create an issue on github repository.
PS: I should have done a Google search properly before writing this one, I saw Anup Gupta has made his efforts in this direction and this plugin has some inspiration from his code for v1.1. But additionally this plugin has CTL key support which I could not see available somewhere else.

#1 by Dharmavirsinh Jhala on February 12, 2011 - 11:56 am
Quote
Issue fixed: Default value (selected ones/edit mode) not working https://github.com/dharmavir/DG-Magnet-Combo/issu…
Updated GitRepos
#2 by Anita on February 10, 2012 - 3:20 am
Quote
DG Magnet Combo is working perfectly.
Could someone please advise how to unselect all options (clear all)? I have tried writing code to deselect all options. It’s working but once I clear all options if I choose an other option it does selecting the previous ones as well.
#3 by Anitha on May 31, 2012 - 9:47 am
Quote
Not working in IE6
#4 by Dharmavirsinh Jhala on June 3, 2012 - 7:21 pm
Quote
Probably because plugin uses jquery data() function and jQuery docs mentions that it won't behave perfectly cross-platform because of the way Internet Explorer works.
Please read more about it on http://api.jquery.com/data/.
Can you try to make a change in the plugin and come up with a snippet which works without data function, I can do that but then I would like if you can come up with that. As it would save some of my time for testing that on IE6 because it will be difficult for me to arrange an IE6 for now.
All are welcome to fork it on https://github.com/dharmavir/DG-Magnet-Combo for all good reasons.
#5 by Anitha on June 5, 2012 - 7:27 am
Quote
Thanks for your suggestion. I have tried changing plugin code without using data() function. I think the problem is not only with the data() function. $(this).val() is not returning the correct value.
I have tried below :
$(this).attr("dgVal", $(this).val() || []); // Set selected values on load if any (reported by fflavio)
$(this).click( function()
{
var oVal = new Array();
var sTop=this.scrollTop;
// oVal=$(this).attr("dgVal") || [];
var nVal=$(this).val();
oVal= [1,2];
alert(oVal);
if(bControlPress != true)
{
if(nVal && nVal.length > 0)
{
var cValIndex = $.inArray(nVal[0],oVal);
if(cValIndex > -1)
{
oVal.splice(cValIndex,1);
}
else
{
oVal.push(nVal[0]);
}
}
$(this).attr("dgVal",oVal).val(oVal);
}
#6 by Anitha on June 6, 2012 - 9:05 am
Quote
you can just install IETester to test in different versions of IE
#7 by Anitha on June 22, 2012 - 8:28 am
Quote
I have managed to fix this issue by changing 'click' to 'change' event. If we use the change event we can't clear the last selection so added 'Clear all' link to clear all selections.
This is working in Jquery vesrion 1.2 but not working until 1.6 and working in the latest Jquery version 1.7.
#8 by parimal on July 16, 2012 - 4:47 pm
Quote
Getting error when there is only one option selected and try to deselect with ctrl button pressed.
error: can't convert null to object