{"id":1175,"date":"2015-01-31T11:40:28","date_gmt":"2015-01-31T11:40:28","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1175"},"modified":"2015-08-10T06:54:39","modified_gmt":"2015-08-10T06:54:39","slug":"jquery-ui-selectable-without-selection-box","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/jquery-ui-selectable-without-selection-box\/","title":{"rendered":"jQuery UI Selectable Without Selection Box"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[1175]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>My <a href=\"http:\/\/stackoverflow.com\/questions\/15930585\/jquery-ui-selectable-without-selection-box\">question<\/a> was:<\/p>\n<p>Given this example here:\u00a0<a style=\"color: #0c65a5;\" href=\"http:\/\/jqueryui.com\/selectable\/#display-grid\" rel=\"nofollow\">http:\/\/jqueryui.com\/selectable\/#display-grid<\/a>\u00a0I would like to make a selection without the &#8220;selection box&#8221; that appears when you click &#8216;n drag. Namely, when I click on number 5 and drag to number 6 and then to 10 I get this:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/z6FcW.jpg\" alt=\"enter image description here\" \/><\/p>\n<p>where in fact what i would like to is drag from 5 to 6 to 10 and have only them selected without 9.<\/p>\n<p>I searched the docs and couldn&#8217;t find that option, and my google skills didn&#8217;t bring me any luck, and I thought this must have been already done it&#8217;s just so happens I can&#8217;t grasp it on my own or find an existing solution, so any help here is appreciated (not saying you should do the research for me, am just hoping someone dealt with this before so he can point me to the right direction).<\/p>\n<p>It also could be I&#8217;m missing the point in trying to accomplish this with jquery UI but this was the first such example I found that fits (kind of) what I want to accomplish.<\/p>\n<p style=\"color: #222222;\">\u00a0The rather extensive answer, by <a style=\"color: #0c65a5;\" href=\"http:\/\/stackoverflow.com\/users\/1558171\/dom\">Dom<\/a>,\u00a0was:<\/p>\n<blockquote><p>First, you might want to hide\u00a0<code>.ui-selectable-helper<\/code>\u00a0or change the CSS:<\/p>\n<pre class=\"lang:default decode:true\">.ui-selectable-helper{display:none;}<\/pre>\n<p>Next, do something like this:<\/p>\n<pre class=\"lang:default decode:true\">$(function(){var _mousedown =false;\r\n    $('#selectable').selectable({\r\n        start:function(event,ui){\r\n            _mousedown=true;},\r\n        stop:function(event,ui){\r\n            _mousedown=false;\r\n            $('.ui-selected').removeClass('ui-selected');\r\n            $('.selecting').addClass('ui-selected');},\r\n        selecting:function(event,ui){if($('.ui-selecting').length ==1)\r\n                $(ui.selecting).addClass('selecting');\r\n\r\n            $('.ui-selecting').removeClass('ui-selecting');\r\n            $('.selecting').addClass('ui-selecting');},\r\n        unselecting:function(event,ui){if($(ui.unselecting).hasClass('selecting'))\r\n                $(ui.unselecting).removeClass('selecting');}});\r\n\r\n    $('#selectable').on('mouseenter','.ui-selectee',function(){if(_mousedown)\r\n            $(this).addClass('selecting');});});<\/pre>\n<p>DEMO:\u00a0<a style=\"color: #0c65a5;\" href=\"http:\/\/jsfiddle.net\/dirtyd77\/7UVNS\/5\/\" rel=\"nofollow\">http:\/\/jsfiddle.net\/dirtyd77\/7UVNS\/5\/<\/a>\u00a0(HELPER HIDDEN)<\/p>\n<p><a style=\"color: #0c65a5;\" href=\"http:\/\/jsfiddle.net\/dirtyd77\/7UVNS\/6\/\" rel=\"nofollow\">http:\/\/jsfiddle.net\/dirtyd77\/7UVNS\/6\/<\/a>\u00a0(HELPER VISIBLE)<\/p>\n<p>Let me know if you have any questions!<\/p>\n<hr style=\"color: #dddddd;\" \/>\n<h2 style=\"font-weight: normal;\">***UPDATE:***<\/h2>\n<p><code>.selectable()<\/code>\u00a0is not able to do what you are looking for. However, here is something I created. Hope it helps!<\/p>\n<p>JAVASCRIPT:<\/p>\n<pre class=\"lang:default decode:true\">$(function(){var _mousedown =false,\r\n        _last=null;    \r\n    $('#selectable li').mousedown(function(){\r\n        _mousedown =true;\r\n        $('.ui-selected').removeClass('ui-selected');\r\n        $('#selectable li').attr('unselectable','on').css('user-select','none');\r\n        $(this).addClass('ui-selecting');}).mouseup(function(){\r\n        _mousedown=false;  \r\n        $('.ui-selecting').addClass('ui-selected').removeClass('ui-selecting');\r\n        $('#selectable li').removeAttr('unselectable style');}).mouseenter(function(){if(_mousedown){if($(this).hasClass('ui-selecting'))\r\n                $(_last).removeClass('ui-selecting');\r\n\r\n            $(this).addClass('ui-selecting')}}).mouseleave(function(){if(_mousedown){\r\n            _last =  $(this)[0];\r\n            $(this).addClass('ui-selecting');}});});<\/pre>\n<p>DEMO:\u00a0<a style=\"color: #0c65a5;\" href=\"http:\/\/jsfiddle.net\/dirtyd77\/7UVNS\/9\/\" rel=\"nofollow\">http:\/\/jsfiddle.net\/dirtyd77\/7UVNS\/9\/<\/a><\/p>\n<hr style=\"color: #dddddd;\" \/>\n<h2 style=\"font-weight: normal;\">***UPDATE #2:***<\/h2>\n<p>If you want to use this on a mobile device, I recommend changing up the format entirely to avoid any possible pitfalls. Here is how I would go about it:<\/p>\n<p>JAVASCRIPT:<\/p>\n<pre class=\"lang:default decode:true  \">$(function(){var _clicked =false;\r\n    $('#btn_select').click(function(){\r\n        _clicked =false;\r\n        $(this).hide();\r\n        $('#selectable li').removeAttr('unselectable style');\r\n        $('.ui-selecting').addClass('ui-selected').removeClass('ui-selecting');});\r\n    $('#selectable li').click(function(){if(!_clicked){\r\n            _clicked =true;\r\n            $('.ui-selected').removeClass('ui-selected');\r\n            $('#selectable li').attr('unselectable','on').css('user-select','none');\r\n            $('#btn_select').show();}if($(this).hasClass('ui-selecting')){\r\n            $(this).removeClass('ui-selecting');}else{\r\n            $(this).addClass('ui-selecting');}});});<\/pre>\n<p>*NOTE: you might want a\u00a0<code>$('body').click()<\/code>\u00a0to act as the end_selection button.<\/p>\n<p>DEMO:\u00a0<a style=\"color: #0c65a5;\" href=\"http:\/\/jsfiddle.net\/dirtyd77\/7UVNS\/13\/\" rel=\"nofollow\">http:\/\/jsfiddle.net\/dirtyd77\/7UVNS\/13\/<\/a><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts I will will be posting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":609,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1175","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1175"}],"version-history":[{"count":3,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1175\/revisions"}],"predecessor-version":[{"id":1693,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1175\/revisions\/1693"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/609"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}