Skip to content

Instantly share code, notes, and snippets.

@UziTech
Created November 21, 2014 07:25
Show Gist options
  • Save UziTech/56e47d6cb7bbbf48853a to your computer and use it in GitHub Desktop.
Save UziTech/56e47d6cb7bbbf48853a to your computer and use it in GitHub Desktop.
HTML Select multiple without ctrl key
/**
* DWTFYW License
* Author: Tony Brix, http://tonybrix.info
*
* Set all multiple select elements to allow selecting multiple without using the ctrl key
*
*/
(function($){
$.fn.selectMultiple = function(){
return this.mousedown(function(e){
e.preventDefault();
//save scrollTop to prevent scrolling on selection change.
//see: http://stackoverflow.com/questions/24543862/selecting-multiple-from-an-html-select-element-without-using-ctrl-key/
var scroll = this.scrollTop;
e.target.selected = !e.target.selected;
this.scrollTop = scroll;
$(this).focus();
}).mousemove(function(e){e.preventDefault()});
};
})(jQuery);
$("select[multiple]").selectMultiple();
@albertoha94
Copy link

Tried it and didn't work :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment