Why jQuery ui selectable plugin is disabling my original bound click function

Viewed 45

i have problem with normal click when i attach jquery.ui-selectable to a row

jquery-ui-selectable is working fine as per desired but my attached click is not working

i have read somewhere to do

e.metaKey = false; //true i did both, but did not work

My code looks like this in jsfiddle

 $('#selectable').selectable({filter:'li'})

/* $('#selectable').bind("mousedown", function (e) {
           e.metaKey = true;
 }).selectable() */
 
 
 $('#selectable li').bind("click", function (e) {
    alert('clicked item : '+$(this).text());
});

Question: i want my attached click to work normally as we expect.

here is demo: http://jsfiddle.net/eabangalore/71xvp5ko/

I have similar code in my project where click is not working

Note: i want my answer to be working in jsfiddle, here in snippet it works don't know why, i my project it does not work

1 Answers

If you want actual click function to then you need to add pointer-events:none to this class ui-selectable-helper so that click event is NOT bubbling up.

Live Demo:

$('#selectable').selectable({
  filter: 'li',
})

$("#selectable > li").on('click',function() {
  console.log($(this).text());
});
#feedback {
  font-size: 1.4em;
}

#selectable .ui-selecting {
  background: #FECA40;
}

#selectable .ui-selected {
  background: #F39814;
  color: white;
}

#selectable {
  list-style-type: none;
  margin: 0;
  padding: 0;
  width: 60%;
}

#selectable li {
  margin: 3px;
  padding: 0.4em;
  font-size: 1.4em;
  height: 18px;
}

.ui-selectable-helper {
  pointer-events: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<ol id="selectable">
  <li class="ui-widget-content">Item 1</li>
  <li class="ui-widget-content">Item 2</li>
  <li class="ui-widget-content">Item 3</li>
  <li class="ui-widget-content">Item 4</li>
  <li class="ui-widget-content">Item 5</li>
  <li class="ui-widget-content">Item 6</li>
  <li class="ui-widget-content">Item 7</li>
</ol>

You can simply use selected method of jQuery selectable plugin - there is no need to do additional bind or click functions to get the clicked item li text() as you wanted.

Live Working Demo:

$('#selectable').selectable({
  filter: 'li',
  selected: function(event, ui) {
    var selectedLi = $(ui.selected).text()
    console.log(selectedLi); //clicked li item text
  }
})
#feedback {
  font-size: 1.4em;
}

#selectable .ui-selecting {
  background: #FECA40;
}

#selectable .ui-selected {
  background: #F39814;
  color: white;
}

#selectable {
  list-style-type: none;
  margin: 0;
  padding: 0;
  width: 60%;
}

#selectable li {
  margin: 3px;
  padding: 0.4em;
  font-size: 1.4em;
  height: 18px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<ol id="selectable">
  <li class="ui-widget-content">Item 1</li>
  <li class="ui-widget-content">Item 2</li>
  <li class="ui-widget-content">Item 3</li>
  <li class="ui-widget-content">Item 4</li>
  <li class="ui-widget-content">Item 5</li>
  <li class="ui-widget-content">Item 6</li>
  <li class="ui-widget-content">Item 7</li>
</ol>

Related