1

I have JQuery UI Sortable+Draggable lists, and i use JQuery .click() for all sortable items, but if i drop new item from draggable list to sortable list, this item do not trigger .click() event.

$(#sortable li).click(function(){
    alert('Sortable item clicked');
});

https://jsfiddle.net/1so55b0t/2/

Why?


Okay, i'm replace .click() to .on('click')

And my elements have close button with class .remove and again this does not work for new elements:

$('#sortable li').on('click','.remove',function(){
    $(this).parent().remove();
});

https://jsfiddle.net/1so55b0t/4/ Why?

PlayerKillerYKT
  • 294
  • 2
  • 12
  • Your jsfiddle sample seems to work.. Are you sure that you enclosed your #sortable li as a string? $("#sortable li") – JimboSlice Aug 18 '16 at 05:20
  • Oh GOD... this question have been asked trillion times here on SO, why dont you search a bit – KAD Aug 18 '16 at 05:21

3 Answers3

4

Try this : As you are moving the lis within DOM, it will not applied to previously bind click event handler. Better use click event delegation, which is useful for dynamic / new elements like below

$('#sortable').on('click', 'li', function(){
    alert('Sortable item clicked');
});

EDIT - Use below code to add click handler for remove buttons -

$('#sortable').on('click','li .remove',function(e) {
    e.stopPropagation();
    $(this).parent().remove();
});

JSFiddle Demo

Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
0

Because you need to append click handler to the newly added elements too. Change you code to as below. Check demo - https://jsfiddle.net/1so55b0t/3/.

$('#sortable').on('click', 'li', function() {
    alert('Sortable item Clicked!');
 });
Nikolay Ermakov
  • 5,031
  • 2
  • 11
  • 18
0

you may use this code

 $(document).on('click', '#sortable li', function () {
 
     alert('Sortable item Clicked!');
     });
Manu Padmanabhan
  • 555
  • 1
  • 4
  • 16