Created
November 21, 2014 07:25
-
-
Save UziTech/56e47d6cb7bbbf48853a to your computer and use it in GitHub Desktop.
HTML Select multiple without ctrl key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tried it and didn't work :(