Permalink
Browse files

Merge pull request #43 from chtrinh/bugfix/double_onselect_callback

Fix bug causing onselected to be fired twice and fix formmatting
  • Loading branch information...
2 parents 436bc89 + aa6af20 commit 2555da768c03af6445fffcb6bac63eeb609459f8 @FezVrasta committed on GitHub Jun 14, 2016
Showing with 10 additions and 19 deletions.
  1. +10 −19 jquery.dropdown.js
View
@@ -80,9 +80,6 @@
$dynamicInput.find("input").attr("placeholder", options.dynamicOptLabel);
$ul.append($dynamicInput);
}
-
-
-
// Cache the dropdown options
var selectOptions = $dropdown.find("li");
@@ -91,11 +88,10 @@
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()
@@ -128,8 +124,6 @@
// 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) {
@@ -141,7 +135,6 @@
return false;
}
});
-
$ul.on("focus", "li:not(.dropdownjs-add)", function() {
if ($select.is(":disabled")) {
return;
@@ -183,8 +176,7 @@
var $selected;
if ($select.find(":selected").length) {
$selected = $select.find(":selected").last();
- }
- else {
+ } else {
$selected = $select.find("option, li").first();
}
methods._select($dropdown, $selected);
@@ -280,10 +272,10 @@
// 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"));
- })
+ });
// Add or remove the value from the input
var text = [];
selectOptions.each(function() {
@@ -298,7 +290,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");
@@ -317,11 +309,10 @@
}
}
- // 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"));
+ }
},
_addOption: function($ul, $this) {
// Create the option

0 comments on commit 2555da7

Please sign in to comment.