From 0d84a65ba2d715d0a7125abf335808e5c556deb9 Mon Sep 17 00:00:00 2001
From: Denis Sedchenko <ascii@live.ru>
Date: Thu, 16 Jun 2016 10:27:46 +0300
Subject: [PATCH 1/4] Watch removed items from 'select'

---
 jquery.dropdown.js | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)

diff --git a/jquery.dropdown.js b/jquery.dropdown.js
index 29c4bfe..2af664e 100644
--- a/jquery.dropdown.js
+++ b/jquery.dropdown.js
@@ -80,6 +80,9 @@
           $dynamicInput.find("input").attr("placeholder", options.dynamicOptLabel);
           $ul.append($dynamicInput);
         }
+
+
+
         // Cache the dropdown options
         var selectOptions = $dropdown.find("li");
 
@@ -88,10 +91,11 @@
             var $selected;
             if ($select.find(":selected").length) {
                 $selected = $select.find(":selected").last();
-            } else {
+            }
+            else {
                 $selected = $select.find("option, li").first();
+               // $selected = $select.find("option").first();
             }
-
             methods._select($dropdown, $selected);
         } else {
             var selectors = [], val = $select.val()
@@ -124,6 +128,8 @@
         // On click, set the clicked one as selected
         $ul.on("click", "li:not(.dropdownjs-add)", function(e) {
           methods._select($dropdown, $(this));
+          // trigger change event, if declared on the original selector
+          $select.change();
         });
         $ul.on("keydown", "li:not(.dropdownjs-add)", function(e) {
           if (e.which === 27) {
@@ -135,6 +141,7 @@
             return false;
           }
         });
+
         $ul.on("focus", "li:not(.dropdownjs-add)", function() {
           if ($select.is(":disabled")) {
             return;
@@ -167,6 +174,11 @@
 
         });
 
+        $select.on("DOMNodeRemoved", function(e) {
+          var deletedValue = e.target.getAttribute('value');
+          $ul.find("li[value='"+deletedValue+"']").remove();
+        });
+
         // Update dropdown when using val, need to use .val("value").trigger("change");
         $select.on("change", function(e) {
           var $this = $(e.target);
@@ -176,7 +188,8 @@
             var $selected;
             if ($select.find(":selected").length) {
               $selected = $select.find(":selected").last();
-            } else {
+            }
+            else {
               $selected = $select.find("option, li").first();
             }
             methods._select($dropdown, $selected);
@@ -272,7 +285,7 @@
         // Toggle option state
         $target.toggleClass("selected");
         // Toggle selection of the clicked option in native select
-        $target.each(function() {
+        $target.each(function(){
           var $selected = $select.find("[value=\"" + $(this).attr("value") + "\"]");
           $selected.prop("selected", $(this).hasClass("selected"));
         });
@@ -290,7 +303,7 @@
       if (!multi) {
         // Unselect options except the one that will be selected
         if ($target.is("li")) {
-          selectOptions.not($target).removeClass("selected");
+            selectOptions.not($target).removeClass("selected");
         }
         // Select the selected option
         $target.addClass("selected");
@@ -309,10 +322,14 @@
         }
       }
 
-      // Call the callback
-      if (this.options.onSelected) {
-        this.options.onSelected($target.attr("value"));
-      }
+       // Call the callback
+        if (this.options.onSelected) {
+            this.options.onSelected($target.attr("value"));
+        }
+
+    },
+    _removeOption: function ($ul, $this) {
+      console.warn(arguments);
     },
     _addOption: function($ul, $this) {
       // Create the option

From 72f8dc6281c329994830731a8338508c07979863 Mon Sep 17 00:00:00 2001
From: Denis Sedchenko <ascii@live.ru>
Date: Thu, 16 Jun 2016 10:28:42 +0300
Subject: [PATCH 2/4] Remove unused test code

---
 jquery.dropdown.js | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/jquery.dropdown.js b/jquery.dropdown.js
index 2af664e..bd7f777 100644
--- a/jquery.dropdown.js
+++ b/jquery.dropdown.js
@@ -328,9 +328,6 @@
         }
 
     },
-    _removeOption: function ($ul, $this) {
-      console.warn(arguments);
-    },
     _addOption: function($ul, $this) {
       // Create the option
       var $option = $("<li></li>");

From 8cc521493ad782944b802a3eda7ab03539ae9938 Mon Sep 17 00:00:00 2001
From: Denis Sedchenko <ascii@live.ru>
Date: Thu, 16 Jun 2016 11:05:19 +0300
Subject: [PATCH 3/4] Change items selection on option remove

---
 jquery.dropdown.js | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/jquery.dropdown.js b/jquery.dropdown.js
index bd7f777..fe8de70 100644
--- a/jquery.dropdown.js
+++ b/jquery.dropdown.js
@@ -20,6 +20,9 @@
       "autoinit": false,
       "callback": false,
       "onSelected": false,
+      "destroy": function(element) {
+        this.destroy(element);
+      },
       "dynamicOptLabel": "Add a new option..."
     },
     init: function(options) {
@@ -177,6 +180,18 @@
         $select.on("DOMNodeRemoved", function(e) {
           var deletedValue = e.target.getAttribute('value');
           $ul.find("li[value='"+deletedValue+"']").remove();
+          var $selected;
+
+          setTimeout(function () {
+            if ($select.find(":selected").length) {
+              $selected = $select.find(":selected").last();
+            }
+            else {
+              $selected = $select.find("option, li").first();
+            }
+            methods._select($dropdown, $selected);
+          }, 100);
+
         });
 
         // Update dropdown when using val, need to use .val("value").trigger("change");

From eeaa3015ccf264ec7c209799b194f7d0e1d47340 Mon Sep 17 00:00:00 2001
From: Denis Sedchenko <ascii@live.ru>
Date: Thu, 16 Jun 2016 11:05:34 +0300
Subject: [PATCH 4/4] Add 'destroy' function

---
 jquery.dropdown.js | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/jquery.dropdown.js b/jquery.dropdown.js
index fe8de70..72ba0c5 100644
--- a/jquery.dropdown.js
+++ b/jquery.dropdown.js
@@ -382,10 +382,14 @@
       } else {
         $ul.append($option);
       }
+    },
+    destroy: function($e) {
+      $($e).show().removeAttr('data-dropdownjs').next('.dropdownjs').remove();
     }
   };
 
   $.fn.dropdown = function(params) {
+    if( typeof methods[params] == 'function' ) methods[params](this);
     if (methods[params]) {
       return methods[params].apply(this, Array.prototype.slice.call(arguments,1));
     } else if (typeof params === "object" | !params) {
