Permalink
Browse files

Fix bug causing onselected to be fired twice and fix formmatting

  • Loading branch information...
1 parent 436bc89 commit b227a5e2b4327547f7c9abe0c1d7fcf402dc7b43 Chris Trinh committed May 12, 2016
Showing with 11 additions and 19 deletions.
  1. +11 −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);
@@ -261,6 +253,7 @@
},
select: function(target) {
var $target = $(this).find("[value=\"" + target + "\"]");
+ console.log("called from select");
methods._select($(this), $target);
},
_select: function($dropdown, $target) {
@@ -280,10 +273,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 +291,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 +310,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 b227a5e

Please sign in to comment.